JavaScript's object model is implemented based on prototypes. Its characteristic is that it is simple. The disadvantage is that it is more difficult to understand than the traditional class-instance model. The biggest disadvantage is that the inherited implementation requires a lot of code to be written and the prototype chain needs to be implemented correctly.
Is there a simpler way to write it? have!
New keywordsclass
It has been officially introduced into JavaScript since ES6.class
The purpose is to make it easier to define the class.
Let's first review the method of implementing Student using functions:
function Student(name) { = name; } = function () { alert('Hello, ' + + '!'); }
If using new oneclass
Write keywordsStudent
, you can write it like this:
class Student { constructor(name) { = name; } hello() { alert('Hello, ' + + '!'); } }
You can find out after comparison.class
The definition contains the constructorconstructor
and functions defined on prototype objectshello()
(Note thatfunction
Keywords), this will avoid = function () {...}
Such a scattered code.
Finally, creating a Student object code is exactly the same as the previous chapter:
var xiaoming = new Student('Xiao Ming'); ();
class inheritance
Another huge benefit of defining objects with class is that inheritance is more convenient. Think about us fromStudent
Derived onePrimaryStudent
The amount of code to be written. Now, the intermediate objects inherited by the prototype, the constructor of the prototype object, etc. need not be considered, and it is directly passedextends
To achieve:
class PrimaryStudent extends Student { constructor(name, grade) { super(name); // Remember to use super to call the constructor of the parent class! = grade; } myGrade() { alert('I am at grade ' + ); } }
NoticePrimaryStudent
The definition of class keyword is also implemented, andextends
It means that the prototype chain object comes fromStudent
. The constructor of a subclass may be different from that of a parent class, for example,PrimaryStudent
needname
andgrade
Two parameters, and need to passsuper(name)
To call the constructor of the parent class, otherwise the parent classname
The attribute cannot be initialized normally.
PrimaryStudent
The parent class has been automatically obtainedStudent
ofhello
Method, we define a new one in the subclassmyGrade
method.
What is the difference between inheriting the class introduced by ES6 and the original JavaScript prototype? In fact, there is no difference between them. The role of class is to allow the JavaScript engine to implement the prototype chain code that we originally needed to write ourselves. In short, the advantage of using class is that it greatly simplifies the prototype chain code.
You will definitely ask, class is so easy to use, can it be used now?
It's still a little too early to use it now, because not all mainstream browsers support ES6 class. If you have to use it now, you need a toolclass
Convert code to traditionalprototype
For code, you can try the Babel tool.
Practice
Please useclass
RedefineCat
and let it go from the existing oneAnimal
Inherit, then add a new methodsay()
, return the string'Hello, xxx!'
。