"Front-end learning notes" call, apply, bind method

Source: Internet
Author: User

1.call () Method:

The move function implements a point position function on the moving plane graph var move = function (x, y) {    this.x + = x;    This.y + = y;}  set a point pvar p = {x:1, y:1};//Call method is called, at which point P will move directly. Move.call (p,1,2); Console.log (P); --{X:2,y:3}

2.apply () Method:

The move function implements a point position function on the moving plane graph var move = function (x, y) {    this.x + = x;    This.y + = y;}  set a point pvar p = {x:1, y:1};//Call the Apply method, at which point P will move directly. Move.apply (p,[1,2]); Console.log (P); --{X:2,y:3}

3.bind () Method:

The/** function as the return value--bind **///move function implements a point position function on the move plane graph var move = function (x, y) {    this.x + = x;    This.y + = y;} //Set a point pvar p = {x:1, y:1}; The bind method receives a parameter and returns a Function procedure that accepts the remaining arguments. The P point does not move at this point. var pmove0 = Move.bind (p); Console.log (P); --{x:1,y:1}////then P moves to the (2,3) position. (PMOVE0); Console.log (P); ----{X:2,y:3}
Of course you can also call var pmove1 = Move.bind (p,1), Console.log (p);p move1 (2); Console.log (P);
Alternatively, call var pmove2 = Move.bind (p,1,2), Console.log (p);p move2 (); Console.log (P);
Bind () compatible notation if (! Function.prototype.bind) {//If there is no bind method on the prototype  Function.prototype.bind = Function (context) {     var args = arguments, Gets all parameters to be passed        obj = arguments[0],//Gets the context to bind        func = this;//gets the function to call    return functions () {//Returns a new function        var ARGC = Array.prototype.slice.call (args,1); Gets the remaining incoming parameters of bind        Array.prototype.push.apply (argc,arguments);//Put the arguments on the call to the last        return func.apply (OBJ,ARGC); Use the new this to execute the Func function}}}     

4.call (), apply () method change this point

Example calls the Apply () method, the call () method is the same as function test () {console.log (' I am Test ');} var obj ={move:function (x, y) {console.log (×); Console.log (y); Console.log (this);//--obj{move:function () {}}}} Obj.move (44,55);//Call the Apply Method Obj.move.apply (test,[66,88]); --function test () {console.log (' I am Test ');}

5.bind () method Change this point

Call the Bind () method function test () {console.log (' I am Test ');} var obj ={move:function (x, y) {console.log (×); Console.log (y); Console.log (this);}} Obj.move (44,55);//This--obj{move:function () {}}var testmove = obj.move.bind (test); Testmove (66,88); This--function test () {console.log (' I am Test ');};

"Front-end learning notes" call, apply, bind method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.