This article shares with you some personal knowledge about the javascript prototype chain.Recommended for everyone here,Hope everyone likes it

JavaScript borrows the characteristics of many languages;For example, the syntax class java, function borrows scheme, prototype inheritance borrows from self, and regular expression borrows from perl. (Dc javascript:language essence).

First, each js is an object-oriented language based on prototype inheritance.Arrays are objects, functions are objects, and "objects" are, of course, objects.And each object has an internal slot [[prototype]], which is the key to the prototype chain connection.True, we can set prototype properties for an object, but what about it,This is just appearance;There is a hidden killer behind.

OK, then I can use isprototypeof () to check if an object is a prototype of another object;However this is also based on the [[prototype]] chain.

for example:

   //Build a function
    function foo () {}
    //Modify the prototype property of the function
     //Create an instance
      var a=new foo ();
    //Override the default prototype of a,It should have been foo.prototype.

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

This should be viewed separately:on the one hand, a.prototype is indeed {name:"a.prototype"};however, foo.prototype.isprototypeof (a) 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 fuse.prototype object. The function.prototype object is "function" and does not contain [[construct]] internally, so it cannot be used as a constructor;In fact function.prototype is similar:function () {}. The "function" type has a prototype property in addition to the [[prototype]] internal slot. Each function is always accompanied by a prototype object:this.prototype={constructor:this} (an ordinary object). [[Prototype]] of this ordinary object is connected to object.prototype.

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

[[Prototype]] of this instance is initialized by the prototype property of the constructor,Note that [[prototype]] is not a function. So if it is an object constructed by the object function,Then it is indeed.

object is a function, and its prototype is the well-known object.prototype (somewhat nonsense), but its [[prototype]] points to function.prototype. See below:

object ----->function.prototype ------>object.prototype.

How to change this [[prototype]] chain?

Can take the form of var a=object.create (obj) or object.setprototypeof (obja, objb). I do n’t want to give examples,Because the relationship is simple;Besides, I can only give some bad examples.Pointless.

last question,Is behavior delegation based on the [[prototype]] chain?

Yes, it is the same.

  • Previous C # simple example of calling a Python script
  • Next CI framework integrated widget (page layout) method