JavaScript draws on the characteristics of many languages; for example, syntax Java, functions draw on Scheme, prototype inheritance draws from Self, and regular expression draws from Perl. (DC Javascript: Language Essence).
First, each JS is an object-oriented language based on prototype inheritance. The array inside is an object, the function is an object, and of course the "object" is still an object. Moreover, each object has an internal slot[[prototype]], which is the key to connecting the prototype chain. Admittedly, we can set a prototype property for an object, but so what? This is just a manifestation; there is a hidden murderous intent behind it.
OK, then I can use isPrototypeOf() to check whether an object is a prototype of another object; however, this is also based on the [[prototype]] chain.
For example:
//Create a function function foo () {} //Modify the function's prototype property foo.prototype = { name : "foo.prototype" }; //Create an instance var a = new foo(); //Rewrite the default prototype of a, which should have been foo.prototype. a.prototype = { name : "a.prototype" };The following question is whether foo.prototype is the prototype of a? !
This needs to be viewed separately: on the one hand a.prototype is indeed { name : "a.prototype"}; however, foo.prototype.isPrototypeOf(a) result is true.
Let’s take a look at the specific relationship: (using ---> means an insignificant [[prototype]] chain, ---- means a prototype property relationship)
Function ---> Function.prototype--->Object.prototype
Function.prototype <--- foo----foo.prototype ------>Object.prototype .
In addition, [[protptype]] of Number, Boolean, String, etc. is still a Function.prototype object. The Function.prototype object is "function", and does not contain [[construct]] inside, so it cannot be used as a constructor; in fact, Function.prototype is similar: function () {}. In addition to [[prototype]]internal slot, the "function" type also has a prototype attribute. Each function always has a prototype object: this.prototype = {constructor:this} (a normal object). The [[prototype]] of this normal object is connected to the Object.prototype.
Is the [[prototype]] of the instance object created by the constructor an Object.prototype?
The [[prototype]] of this instance is initialized by the prototype property of the constructor. Note that it is not the [[prototype]] of the function. So if it is an object constructed by the Object function, then it is indeed.
Object is a function, its prototype is the famous Object.prototype (a bit nonsense), but its [[prototype]] points to Function.prototype. Please see below:
Object----->Function.prototype------>Object.prototype.
How to change this [[prototype]] chain?
It can be in the form of var a = Object.create(obj) or Object.setPrototypeOf(objA,objB). I don't think there is any need to give examples, because the relationship is very simple; let's only give some crappy examples. No sense.
The last question is, is the behavioral delegation based on the [[prototype]] chain?
Yes, that's the case.