Does JavaScript require classes?

Source: Internet
Author: User

Original article: http://www.nczonline.net/blog/2012/10/16/does-javascript-need-classes/

Note: During my one-year career, I have met someone who calls constructors as classes, and someone who calls objects as classes. this is more painful than calling Firefox extensions as plug-ins. the following is the JSCONF file provided by Brendan eich for this year. the video recorded by EU mentioned the class.

Whether you like it or not, ecmascript 6 will include the new class [1]. in JavaScript, the demand for classes has always been polarized. some people especially like JavaScript because it is different from other languages. on the other hand, some people hate JavaScript, because it is different from other languages. one of the psychological barriers that people need to overcome most from C ++ or Java to Javascript is "no class in JavaScript". Some people have told me that, this is one of the reasons why they do not like JavaScript or continue to learn Javascript in depth.

 

Javascript has no real classes since its invention, which has caused confusion from the very beginning. There are many JavaScript books orArticleClasses are all mentioned, just as there are classes in JavaScript. however, the classes they call are just some custom constructor functions that can be used to construct custom reference types. in JavaScript, the reference type is already the closest thing to the class. the followingCodeYou should be familiar:

FunctionMycustomtype (value ){This. Property =Value;} mycustomtype. Prototype. Method=Function(){Return This. Property ;};

This code is often interpreted as declaringMycustomtypeClass. But in fact, all the things the Code does is declareMycustomtypeFunctionWith the new operator, you can use it to create a reference type.Mycustomtype instance. This function is essentially different from other functions, because other custom functions can also be used as constructors.

 

On the surface, such code does not seem to be defining a class at all. The defined constructor does not seem to have any connection with the methods on its prototype object. if you are a beginner in Javascript, you may think that this is two completely unrelated codes. but in fact, these two pieces of code are very closely related, but they are far from writing classes in other languages.

 

what is even more confusing is that when it comes to inheritance, most people think of sub-classes and super classes. Such a concept is meaningful only when a real class exists. in JavaScript, the inherited code is also lengthy:

FunctionAnimal (name ){This. Name =Name;} animal. Prototype. sayname=Function() {Console. Log (This. Name );};FunctionDog (name) {animal. Call (This, Name);} dog. Prototype=NewAnimal (Null); Dog. Prototype. Bark=Function() {Console. Log ("Woof! ");};

There are two steps to implement inheritance: Creating constructors and rewriting prototype objects. This code is messy.

 

In the first version of JavaScript advancedProgramIn design, I used the term "class ". however, from the feedback I received, this writing is confusing. so in the second version, I replaced all "classes" with "type )". it turns out that using the term "type" can reduce a lot of confusion.

 

However, there is another prominent problem: the syntax for defining a custom type is lengthy, and inheritance between two types is more complicated. there is no simple way to call a super-type method. at present, it is very painful to create and manage a custom type. If you do not believe it, you can see how many of the following JavaScript frameworks use their own custom types and inheritance methods:

    • Yui-UseY. Extend () to implement inheritance. This method will add"Superclass "attribute.[2]
    • Prototype-UseClass. Create () andObject. Extend () to process objects and "classes". [3]
    • Dojo-UseDojo. Declare () andDojo. Extend (). [4]
    • Mootools-There is a custom typeClass, which can be used to define and extend "class". [5]

So many JavaScript frameworks have their own solutions, which is obviously very confusing. Javascript developers need a better syntax to implement this function.

 

The classes in ecmascript 6 do not actually have any new things. They just add a syntactic sugar in the mode you are already familiar with. Let's take a look at the example below:

 
Class mycustomtype {Constructor (value ){This. Property =Value;} method (){Return This. Property ;}}

The class definition in ecmascript 6 is actually another way of writing the mycustomtype example above. the object instance created using this type is exactly the same as the object instance created using the constructor. the only difference is that the former has a more compact syntax. let's take a look at the inheritance Syntax:

Class animal {Constructor (name ){This. Name =Name;} sayname () {console. Log (This. Name) ;}} class dog extends animal {Constructor (name) {super (name);} bark () {console. Log ("Woof! ");}}

In practice, this example is also equivalent to the previous method of inheritance, but a complicated implementation step is a simpleThe extends keyword is replacedIn the class definition, you can also directly useSuper (), no need to explicitly specify the super-Type constructor.

 

The classes in ecmascript 6 are based on the Javascript mode you are already familiar. the principle of implementation inheritance is also the same as before (based on the prototype chain, calling a super-Type constructor). Methods are added to the prototype, and attributes are declared in the constructor. there is only one real difference: You can enter less words.

 

Therefore, if you still do not agree with the introduction of class in ecmascript 6, you can think that the class to be introduced is not a new thing, it does not fundamentally change the working mechanism of JavaScript. however, I personally recommend using keywords.Type insteadClass.

 

So does JavaScript really need classes? The answer is no, but JavaScript does need a more concise method to create a custom type. this happens to be what the "class" in ecmascript 6 is about to do. if this "class" helps developers from other languages to turn JavaScript more easily, it is a good thing.

 

Reference
    1. Maximally minimal classes (ECMA)
    2. Yui extend () (yuilibrary)
    3. Prototype classes and inheritance (prototype)
    4. Creating and enhancing dojo classes (sitepen)
    5. Mootools class (mootools)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.