A Personal Understanding of JavaScript Prototype Chain

  • 2021-07-06 09:59:35
  • OfStack

JavaScript draws on the characteristics of many languages; For example, syntax class Java, function from Scheme, prototype inheritance from Self, regular expression from Perl. (DC Javascript: The essence of language).

First, each JS is an object-oriented language based on prototype inheritance. The array is an object, the function is an object, and the "object" is of course an object. And each object has an internal slot [[prototype]], which is the key to linking prototype chains. Admittedly, we can set prototype property for one object, but so what? It's just appearances; There are hidden dangers behind.

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:

    // Establish 1 Functions 

    function foo () {} 

    // Modify the function's prototype property  

    foo.prototype = {

         name : "foo.prototype" 
     // Establish 1 Instances  

      var a = new foo();

    // Rewrite  a  The default prototype of, which should be foo.prototype.
       a.prototype = {
         name : "a.prototype"

The following question is whether foo. prototype is a prototype of a? !

This should be looked at separately: 1 aspect a. prototype is indeed {name: "a. prototype"}; However, foo. prototype. isPrototypeOf (a) results in true.

Let's look at 1 to see the specific relationship: (using-- > Represents an inconspicuous [[prototype]] chain,----Represents an prototype property relationship)

Function --- > Function.prototype--- > Object.prototype

Function.prototype < --- foo---foo.prototype ------ > Object. prototype.

In addition, the [[protptype]] of Number, Boolean, String, etc. are still Fuction. prototype objects. The Function. prototype object is "function" and does not contain [[construct]], so it cannot be used as a constructor; Actually Function. prototype is similar: function () {}. The "function" type has an prototype attribute in addition to [[prototype]] internal slot. Each function is always accompanied by an prototype object: this. prototype = {constructor: this} (a normal object). The [[prototype]] of this generic object is connected to Object. prototype.

Is the [[prototype]] of the instance object created by the constructor Object. prototype?

The [[prototype]] of this instance is initialized by the constructor's prototype property, and note that it is not the function's [[prototype]], so if it is an object constructed by the function Object, it is.

Object is a function, and its prototype is the famous Object. prototype (a bit of nonsense), but its [[prototype]] points to Function. prototype. See below:

Object----- > Function.prototype------ > Object.prototype.

How do you 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 I need to give examples, because the relationship is very simple; Besides, I can only give one lame example. It doesn't make sense.

Finally, is the behavioral delegate based on the [[prototype]] chain?

Yes, the same is true.

Related articles: