ECMAScript 6標準將為JavaScript引入類的概念

jieforest發表於2012-10-21
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:

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章