ECMAScript 6標準將為JavaScript引入類的概念
Like it or not, ECMAScript. 6 is going to have classes[1]. The concept of classes in JavaScript. has always been polarizing. There are some who love the classless nature of JavaScript. specifically because it is different than other languages. On the other hand, there are those who hate the classless nature of JavaScript. because it’s different than other languages. One of the biggest mental hurdles people need to jump when moving from C++ or Java to JavaScript. is the lack of classes, and I’ve had people explain to me that this was one of the reasons they either didn’t like JavaScript. or decided not to continue learning.
JavaScript. hasn’t had a formal definition of classes since it was first created and that has caused confusion right from the start. There are no shortage of JavaScript. books and articles talking about classes as if they were real things in JavaScript. What they refer to as classes are really just custom constructors used to define custom reference types. Reference types are the closest thing to classes in JavaScript. The general format is pretty familiar to most developers, but here’s an example:
this.property = value;
}
MyCustomType.prototype.method = function() {
return this.property;
};In many places, this code is described as declaring a class named MyCustomType. In fact, all it does is declare a function named MyCustomType that is intended to be used with new to create an instance of the reference type MyCustomType. But there is nothing special about this function, nothing that says it’s any different from any other function that is not being used to create a new object. It’s the usage of the function that makes it a constructor.
The code doesn’t even look like it’s defining a class. In fact, there is very little obvious relationship between the constructor definition and the one method on the prototype. These look like two completely separate pieces of code to new JavaScript. developers. Yes, there’s an obvious relationship between the two pieces of code, but it doesn’t look anything like defining a class in another language.
JavaScript. hasn’t had a formal definition of classes since it was first created and that has caused confusion right from the start. There are no shortage of JavaScript. books and articles talking about classes as if they were real things in JavaScript. What they refer to as classes are really just custom constructors used to define custom reference types. Reference types are the closest thing to classes in JavaScript. The general format is pretty familiar to most developers, but here’s an example:
CODE:
function MyCustomType(value) {this.property = value;
}
MyCustomType.prototype.method = function() {
return this.property;
};In many places, this code is described as declaring a class named MyCustomType. In fact, all it does is declare a function named MyCustomType that is intended to be used with new to create an instance of the reference type MyCustomType. But there is nothing special about this function, nothing that says it’s any different from any other function that is not being used to create a new object. It’s the usage of the function that makes it a constructor.
The code doesn’t even look like it’s defining a class. In fact, there is very little obvious relationship between the constructor definition and the one method on the prototype. These look like two completely separate pieces of code to new JavaScript. developers. Yes, there’s an obvious relationship between the two pieces of code, but it doesn’t look anything like defining a class in another language.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-746864/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ECMA標準ECMAScript(JavaScript的一個標準)和C#JavaScriptC#
- portlets API將成為標準!API
- Javascript——ES6( ECMAScript 6.0)語法JavaScript
- 在C語言中引入類的概念(轉)C語言
- ECMAScript 6中除類之外的OOP新特性OOP
- JavaScript非同步程式設計(1)- ECMAScript 6的Promise物件JavaScript非同步程式設計Promise物件
- Angular 為什麼要引入 injection token 的概念Angular
- JavaScript ECMAScript 6 筆記 -2 《變數的解構賦值》JavaScript筆記變數賦值
- 將Standard標準叢集修改為Flex叢集Flex
- 玩轉ECMAScript 6 ~
- ECMAScript 6 掃盲
- ECMAScript 6教程 (三)
- JavaScript(ECMAScript) with 語句JavaScript
- ECMAScript 6:更好的 Unicode 支援Unicode
- ECMAScript 6筆記(一)筆記
- ECMAScript6簡介
- #ECMASCRIPT6筆記筆記
- ECMAScript6 教程(一)
- JavaScript各類標籤的使用JavaScript
- JavaScript ECMAScript版本介紹JavaScript
- JavaScript與ECMAScript 區別JavaScript
- JavaScript(ECMAScript+BOM+DOM)JavaScript
- golang標準庫的分析os包(6)Golang
- 類Excel設計器的基本標準Excel
- JavaScript實現標準快速排序JavaScript排序
- Flask(10)- 標準類檢視Flask
- Rust的標準庫為啥很小?Rust
- 微軟將與英特爾聯手為雙屏筆記本以及同類裝置開發共同的標準微軟筆記
- C2x將成為C語言的下一個ISO標準C語言
- 智慧產業就緒指數將成為工業4.0轉型的全球標準產業
- VBA標準模組與類模組(轉)
- 將12c RAC由標準叢集改為flex叢集Flex
- ECMAScript6 實用筆記筆記
- 《ECMAScript 6入門》上線了
- JavaScript:到底什麼是 ES6、ES8、ES 2017、ECMAScript?JavaScript
- DEMO6:座標新增文字標籤的JavaScript外掛JavaScript
- 資料顯示5.5寸將成為手機螢幕的最標準尺寸
- JavaScript的三種引入方式JavaScript