There are three very common functions in native JS, Call,apply,bind
Their role is to change the current function's this pointer,
But they still have different nuances.
1) call,apply executes a function and finds that this has become a time to use (at the time)
2) Bind is forced to change the point of this before the function is called (before), its effect is to return a function (just to change this point)
That's a lot to say.
Demo:
function Foo (name) { This. name=name; } Foo.prototype.getName=function () {return This. Name; } function Bar (Name,label) { Foo.call ( this,name); This. label=label; } Bar.prototype.getMylabel=function () {return This. Label; } varfo=NewFoo ('One light'); Console.log (Fo.getname ()); One lightvarBa=NewBar ("is that yours?","everyone's"); Console.log (Ba.getmylabel ()); Everyone's
Console.log (BA);//bar{label: Everyone, Name: it's yours? }
Wise man will have a problem, this BA how to assign the name value succeeds, because he does not have Foo method?
This is the call of the powerful. Slowly experience one of the Austrian seconds,
a question : How to change call to apply???
Only this is required: Foo.call (This,name), to be changed to--->foo.apply (this,[name]) ..... Say more, applay and call is not the Applay parameter is an array,
The second question: How to change to bind????
It has been said that the use of bind is done before the function is performed, and returns a function
var setname=foo.bind (this);
SetName (name);
Of course, you can also write a sentence: Foo.bind (this) (name); On the surface, it seems like just a parenthesis with call, but the meaning is really different, bind is to return a function first, then execute the function,,,,,
The third question: How do I export the name of my ba???
Very simple: Console.log (Ba.getname ());
Well, wrong, really wrong, BA no GetName () method AH.
What's up, here?
method is not unique.
The first method:
Console.log (Foo.prototype.getName.call (BA));
or Console.log (Foo.prototype.getName.bind (BA) ());
Console.log (Foo.prototype.getName.apply (BA));
The second method:
Bar.prototype=object.create (Foo.prototype);
Console.log (Ba.getname ());//Is it yours?
"The role of Objecrt.create is to associate Foo.prototype with Bar.prototype."
Console.log (Ba.getmylable ()); What the hell is wrong with//error???????
"Forget to say, Object.create () He will create a new object, so that Bar.prototype will be replaced, so embarrassed, Getmylabel () lost .... 】
The clever child paper says: So come on:
Bar.prototype=object.create (foo.prototype); Bar.prototype.getMylabel=function () { returnthis. Label; }
A little trouble is useless, very good.
In fact, can be more elegant point, when you open your textbook, you will find in fact, object.setprototypeof (Bar.prototype,foo.prototype) can avoid the above embarrassing phenomenon,
Object.setprototypeof (Bar.prototype,foo.prototype) will change some things of the former, will not abandon him
I've got nothing to say here, just to be sure, the BIND function has some compatibility issues
The use and difference of JS's call,apply,bind