ApplyInvokes the function and replaces the function's this value with the specified object, replacing the function's arguments with the specified array.
Functionname.apply ([Thisobj[,argarray]])
If Argarray is an invalid value, an "Object expected" error is thrown, and if neither thisobj nor Argarray is provided, the current this is used as the Thisobj
function CallMe (arg1, arg2) {var s = ""; s + = "This value:" + this; s + = "<br/>"; For (i in callme.arguments) {s + = "arguments:" + callme.arguments[i]; s + = "<br/>"; } return s;} document.write ("Original function: <br/>");d Ocument.write (CallMe (1, 2));d ocument.write ("<br/>"); document.write ("Function called with apply: <br/>");d Ocument.write (callme.apply (3, [4, 5]);d Ocument.write (" <br/> ");d ocument.write (" Function called with Apply with invalid array: <br/> "); try{document.write (callMe. Apply (3,2));} catch (E) {document.write (e.message);} document.write ("<br/><br/>");d ocument.write ("Function called with the Apply without any argument: <br/> ");d Ocument.write (callme.apply ());//output result://original function://this value: [Object window]//arguments:1// Arguments:2//function called with apply://this value:3//arguments:4//arguments:5//function called with APPL Y with InvalID Array://function.prototype.apply:arguments list have wrong type//function called with apply without any argument://th Is value: [Object Window]
PagerInvokes the method of an object, replacing the current object with another object.
Call ([thisobj[, arg1[, arg2[, [, ArgN]]])
It allows you to change the function's this object from the initial context to the new object specified by Thisobj. If the Thisobj parameter is not provided, the global object is used as the thisobj. The only difference from the Apply method is that the second argument type of apply must be an array, and the call method enumerates all the arguments, separated by commas.
function CallMe (arg1, arg2) { var s = ""; s + = "This value:" + this; s + = "<br/>"; For (i in callme.arguments) { s + = "arguments:" + callme.arguments[i]; s + = "<br/>"; } return s;} document.write ("Original function: <br/>");d Ocument.write (CallMe (1, 2));d ocument.write ("<br/>"); document.write ("Function called with Call: <br/>");d Ocument.write (Callme.call (3, 4, 5));//Output://Original Function://This value: [Object window]//arguments:1//arguments:2//Function called with Call://This value:3//arg uments:4//Arguments:5
BindFor a given function, create a binding function with the same body as the original function. In the binding feature, the This object resolves to an incoming object. The binding function has the specified initial parameters.
Function.bind (Thisarg[,arg1[,arg2[,argn]])
where function, Thisarg is the required option. Returns a new function that is the same as the function function, except that the this object and the parameter are different.
Define the original Function.var checknumericrange = function (value) { if (typeof value!== ' number ') return F Alse; else return value >= this.minimum && value <= this.maximum;} The Range object would become the this value in the callback Function.var range = {minimum:10, maximum:20};//Bind t He checknumericrange Function.var Boundchecknumericrange = checknumericrange.bind (range);//Use the new function to check Whether in the numeric range.var result = Boundchecknumericrange;d ocument.write (result);//Output:true
The following code shows how to use the ARG1[,ARG2[,ARGN]]] parameter. The binding function uses the parameters specified in the Bind method as the first and second arguments. When the binding function is called, any parameters specified will be used as the third, fourth parameter (and so on).
Define the original function with four Parameters.var Displayargs = function (Val1, Val2, Val3, Val4) {
Use the Bind method inside an object definition to bind an event to a method inside the object.<input type= "button" id= "Start" value= "Start"/><input type= "button" id= "Stop" value= "Stop"/><script Type= "Text/javascript" > Function Car (owner) {this.owner = owner; This.start = function () {//start the car console.log (this); Output:car {owner: "Mike", Start:function, stop:function} check.html:14 Console.log (This.owner + "s Car I s starting. "); Output:mike ' s car is starting. }; This.stop = function () {console.log (this); Output: <input type= "button" id= "Stop" value= "Stop"/> Console.log (This.owner + "s car is starting.") ; Output:undefined ' s car is stopping. }; } var btnstart = document.getElementById ("Start"), Btnstop = document.getElementById ("Stop"), Somecar = n EW Car ("Mike"); if (document.attachevent) {btnstart.attachevent ("OnClick", SomeCar.start.bind (Somecar)); BtnsTop.attachevent ("OnClick", somecar.stop); } else if (Document.addeventlistener) {Btnstart.addeventlistener ("click", SomeCar.start.bind (Somecar), false); Btnstop.addeventlistener ("Click", Somecar.stop, false); }</script>From the above sample we found that when the bind method is not used, the event inside this point of the trigger click event DOM element input, it certainly does not have the Owner property; If you use bind to specify the object inside the event, you can achieve the desired effect.