Today again see JS inheritance, with a new understanding. is actually a further understanding of the scope
First look at an example:
function A (name) {
THIS.name = name;
This.sayhello = function () {alert (this.name+ "Say hello!");
}
function B (name,id) {
This.temp = A;
This.temp (name); Equivalent to New A ();
Delete this.temp; Prevents the properties and methods of superclass a from being overwritten with temp references at a later time
This.id = ID;
This.checkid = function (ID) {alert (this.id==id)};
}
This is the classic object posing, and the two lines of red are equivalent to executing a (name), and this in a is already replaced with this in B.
So what happens when we change the red part directly to A (name)?
So we do the experiment
var B = new B ("B", "BID");
alert (b.name);//undefined
Why.
Because there is no binding scope when executing a, this is still window
We output alert (window.name) or alert (name)//b
So this object is actually a transformation of this, the binding scope to an object
But is there any other way to do this?
Apply and call
The role of personal understanding: calling functions and binding scopes
That's the meaning of the object pretending.