Questions about javascript inheritance
仅有的幸福
仅有的幸福 2017-05-19 10:37:35
0
2
537

If you want to implement object A to inherit B, and B to inherit C, is there any other way to implement it besides assigning the instance of B to the prototype of A, and assigning the instance of C to the prototype of B? I am a beginner and have not found any relevant information. Please give me some advice

仅有的幸福
仅有的幸福

reply all(2)
大家讲道理

In other words, can your question be understood as: "In addition to prototypal inheritance, what other inheritance methods does JavaScript have?" If so, the answer is no, because JavaScript inheritance is implemented based on the prototype chain mechanism.
If you are asking about how to write prototypal inheritance, there are several ways:
1) new constructor creation; (that is, the one mentioned in the question)
2) Object.create;
1) Object literal;
Specific usage , there is a lot of information:
https://developer.mozilla.org...

伊谢尔伦

In addition to prototypal inheritance, there are also APIs provided by js: call and applyChange the pointer of this and borrow methods
call usage:

a.call(b,pro1,pro2,...)

apply Usage:

a.apply(b,[pro1,pro2,...]) 

The difference between the two is that the former passes parameters one by one, while the latter passes parameters by passing an array and then spreading it out

There are other uses for these two, such as converting pseudo arrays into real arrays:
Object.prototype.tostring.call (pseudo array)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template