Home > Web Front-end > JS Tutorial > body text

The relationship between class and js prototype prototype inheritance in ES6

小云云
Release: 2018-03-07 15:27:56
Original
1333 people have browsed it

ES6 introduces the keyword class in order to further reduce code writing and simplify code logic. But the implementation of class is also based on prototype, with a layer of syntactic sugar. Most of its functions can be achieved by ES5. The new class writing method only makes the writing method of object prototype clearer and more like object-oriented programming. Just grammar.

class Person {
constructor(name) {
this.name=name||"Default";
}
toString(){
return 'Name:'+this.name;
}
}
var p1=new Person();
console.log(p1.name);
class Boy extends Person{
constructor(name){
super(name);
this.gende='Boy';
}
toString(){
return super.toString()+" - Gende:"+this.gende;
}
}
var b1=new Boy('hello');
console.log(b1);
console.log(b1.toString());
Copy after login

Inheritance of ES5, that is, prototype
The essence is to first create the instance object this
of the subclass and then add the methods of the parent class to this (Parent. apply(this))
ES6 inheritance, that is, class
The essence is to first create the instance object this of the parent class (super must be called first)
and then use the constructor of the subclass to modify this
Their implementation mechanisms are different.
ES6 is the same as ES5. All instances of a class share a prototype object.

For es6, it is still necessary to read the documentation.

Related recommendations:

Detailed explanation of the class keyword in ES6

Examples of usage of get and set of the class class in ES6 javascript

Detailed explanation of the usage of Prototype attribute of js

The above is the detailed content of The relationship between class and js prototype prototype inheritance in ES6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!