Home > front end >  In JavaScript prototype chain and inheritance usage scenario is what ah?
In JavaScript prototype chain and inheritance usage scenario is what ah?

Time:09-25

JS in asking god detailed said the prototype chain and inheritance should be how to application, under what kind of scene would use the prototype chain and inherit these things, the interview several companies, each technology the interviewer to ask about this aspect of the problem, always don't answer. O great spirit action!

CodePudding user response:

The usual work, really use

Generally in the js object encapsulation, may use, can let your object structure is more clear, the code is more concise, easy to read and maintain,

But now the front-end is modular, modular, packaging has become the norm, familiar with the prototype chain and inheritance, may also be necessary.

Anyway, familiar with this, the more thorough understanding js working principle, can be used to assess a person's level of js,

CodePudding user response:

Es5 and es5 before the javascript version of the prototype chain and prototype can be used to implement inheritance, es6 + a class syntactic sugar and the concept of inheritance, I really don't know what prototype chain, may specify the prototype chain can shorten the Object's properties and methods of the prototype chain lookup process, optimal performance, otherwise a undefined reference Object properties will always follow the prototype chain lookup to the Object,
Inherited usage scenarios, I think, when a project for different Settings such as user equipment types (mobile phone or PC or tablet) write multiple versions can inherit, put some multiple versions of common properties and methods on a parent class, and then other versions inherit the parent class is the parent class attributes and methods, and then write a subclass in subclasses unique attributes and methods, so the code reuse can be achieved, but improve the efficiency of the development, structure and more pure and fresh,

CodePudding user response:

wheatgrass CMS reference 2 floor response:
es5 and es5 before the javascript version of the prototype chain and prototype can be used to implement inheritance, es6 + a class syntactic sugar and the concept of inheritance, I really don't know what prototype chain, may specify the prototype chain can shorten the Object's properties and methods of the prototype chain lookup process, optimal performance, otherwise a undefined reference Object properties will always follow the prototype chain lookup to the Object,
Inherited usage scenarios, I think, when a project for different Settings such as user equipment types (mobile phone or PC or tablet) write multiple versions can inherit, put some multiple versions of common properties and methods on a parent class, and then other versions inherit the parent class is the parent class attributes and methods, and then write a subclass in subclasses unique attributes and methods, so that the code reuse can be achieved, but improve the efficiency of the development, structure and more pure and fresh,

I now is to understand the basic concept of the simple, but in the usual work, really don't know how to use the prototype chain, is it, however, I think you not only understand the very in place, but also is good at application, admire you!

CodePudding user response:

reference qq_35770417 reply: 3/f
Quote: refer to the second floor wheatgrass CMS response:
es5 and es5 before the javascript version of the prototype chain and prototype can be used to implement inheritance, es6 + a class syntactic sugar and the concept of inheritance, I really don't know what prototype chain, may specify the prototype chain can shorten the Object's properties and methods of the prototype chain lookup process, optimal performance, otherwise a undefined reference Object properties will always follow the prototype chain lookup to the Object,
Inherited usage scenarios, I think, when a project for different Settings such as user equipment types (mobile phone or PC or tablet) write multiple versions can inherit, put some multiple versions of common properties and methods on a parent class, and then other versions inherit the parent class is the parent class attributes and methods, and then write a subclass in subclasses unique attributes and methods, so that the code reuse can be achieved, but improve the efficiency of the development, structure and more pure and fresh,

I now is to understand the basic concept of the simple, but in the usual work, really don't know how to use the prototype chain, is it, however, I think you not only understand the very in place, but also is good at application, admire you!

In order to understand the prototype chain es6 - and inheritance, and the relationship between the prototype chain and inheritance, because there is no class es6 - and the concept of inheritance, js implementation inheritance is the essence of the js object constructor abstractions in his mind into a class, and then use the constructor protptype attribute encapsulation of a class (another constructor), to make it perfect (because js implementation inheritance has a lot of people come to the conclusion that, in the defective) inherited a constructor before all of the attributes and methods, because the constructor to new out of a specific object instance, it is in the js modernized of object-oriented and inheritance, to understand the point you,
1. The basic theory of modern object-oriented, such as the basic concept of Java object-oriented,
2. The origin of the prototype and the function of the js, remember is to understand, to get through.
3. Understand the front two, I think you the pros and cons of various implementation inheritance in js performance easily understand, this is the third point,

Language: after the es6, really it is necessary to master the es6 - prototype chain and implementation inheritance?

More than just personal opinion,

CodePudding user response:

For es6 - no classes, and the concept of inheritance, js implementation inheritance is the essence of the js object constructor abstractions in his mind into a class, and then use the constructor protptype attribute encapsulation of a class (another constructor), to make it perfect (because js implementation inheritance has a lot of people come to the conclusion that, in the defective) inherited a constructor before all of the attributes and methods, because the constructor to new out of a specific object instance, it is in the js modernized of object-oriented and inheritance, in order to understand the prototype chain es6 - and inheritance, and the relationship between the prototype chain and inheritance, to understand the 3 points, you
1. The basic theory of modern object-oriented, such as the basic concept of Java object-oriented,
2. The origin of the prototype and the function of the js, remember is to understand, to get through.
3. Understand the front two, I think you the pros and cons of various implementation inheritance in js performance easily understand, this is the third point,

Language: after the es6, really it is necessary to master the es6 - prototype chain and implementation inheritance?

More than just personal opinion,

CodePudding user response:

wheatgrass CMS reference 5 floor response:
for es6 - no classes and the concept of inheritance, js implementation inheritance is the essence of the js object constructor abstractions in his mind into a class, and then use the constructor protptype attribute encapsulation of a class (another constructor), to make it perfect (because js implementation inheritance has a lot of people come to the conclusion that, in the defective) inherited a constructor before all of the attributes and methods, because the constructor to new out of a specific object instance, it is in the js modernized of object-oriented and inheritance, in order to understand the prototype chain es6 - and inheritance, and the relationship between the prototype chain and inheritance, to understand the 3 points, you
1. The basic theory of modern object-oriented, such as the basic concept of Java object-oriented,
2. The origin of the prototype and the function of the js, remember is to understand, to get through.
3. Understand the front two, I think you the pros and cons of various implementation inheritance in js performance easily understand, this is the third point,

Language: after the es6, really it is necessary to master the es6 - prototype chain and implementation inheritance?

More than just personal opinion,


Is it necessary? to
First of all, the emergence of the ES6 not overturned previous prototype inheritance way, it's just past inheritance methods of encapsulation, a syntactic sugar for careful friends would like to learn about the underlying technology will probably go to see ES6 through Babel compiled code eg:
 
//ES6
The class extends B {}
The console. The log (new A)

Below is through the Babel after ES5
 
//ES6=& gt; Babel=& gt; ES5
"Use strict";

The function _getPrototypeOf (o) {
_getPrototypeOf=Object. SetPrototypeOf? Object. GetPrototypeOf: function _getPrototypeOf (o) {
Return o. __proto__ | | Object. GetPrototypeOf (o);
};
Return _getPrototypeOf (o);
}

The function _inherits (ttf_subclass, superClass) {
If (typeof superClass!=="function" & amp; & SuperClass!==null) {
Throw new TypeError (" Super expression must either be null or a function ");
}
Ttf_subclass. Prototype=Object. The create (superClass & amp; & SuperClass. Prototype, {
Constructor: {
Value: ttf_subclass,
Writable: true,
Configurable: true
}
});
If (superClass) _setPrototypeOf (ttf_subclass, superClass);
}

The function _setPrototypeOf (o, p) {
_setPrototypeOf=Object. SetPrototypeOf | | function _setPrototypeOf (o, p) {
O. __proto__=p;
return o;
};
Return _setPrototypeOf (o, p);
}

Var A=
/* # __PURE__ */
The function (_B) {
_inherits (A, _B);
Function A () {
Return _getPrototypeOf (A). The apply (this, the arguments);
}
Return A;
} (B);
The console. The log (new A ());

The underlying base determines the superstructure ~
  • Related