Js:this,call,apply,bind Summary

Source: Internet
Author: User

JS in some of the basic concepts to do some summary, to further study JS is very heavy.

First, this

The this in JavaScript always points to an object, pointing to that object being dynamically bound at run time based on the execution environment of the function, not when the function is declared.
The direction of this in practice can be broadly divided into the following 4:
1. Method calls as objects
2. Fall off as a normal function
3. Constructor invocation
4. Function.prototype.call or Function.prototype.apply call, can dynamically change the access function of this

  1. When called as a method of an object, this points to the object

1 var obj = {2     a:1,3     function() {4 This         //  true5          This // 1 6     }7}; 8 Obj.geta ();

2. As a normal function drop, this always points to the Global object window

1 console.log (this//  Windows23 window.name = "Globalname"  ; 4 var function () {5     return  This . Name; 6 }78//  globalname

  3. Constructor invocation, when a function is called with the new operator, the function always returns an object, usually the this in the constructor points to the returned object

1 var function () {2     this. Name = "Class";     3 }4varnew  MyClass (); 5 // class

If you call the constructor with new, the constructor explicitly returns an object of Type objects, then the result of this operation will eventually return this object, not the one I expected before

 1  var  MyClass = function   () { 2  this . Name = "Class" ;  3  return   { 4  name: "Other" 5   6    var  obj = new   MyClass ();  8  Console.log (obj.name); //  other  

Ii. Call and apply

Their role is exactly the same, and the difference is only in the form of incoming parameters.
Apply receives two parameters, the first parameter specifies the pointer to the this object in the function body, the second parameter is an indexed collection, which can be an array or an array of classes, and the Apply method passes the elements in the collection as arguments to the called function.
Call incoming parameters are not fixed, the same as apply, the first parameter also represents this point in the body of the function, starting from the second argument, each parameter is passed to the function sequentially

1 var function (A, B, c) {2    console.log ([A, B, c]); 3 }4// The first parameter passed in is null, and this in the body of the function points to the default host object, which in the browser is window5 Func.apply (null//  output: [1, 2, 3]67 func.call (null // output: [1, 2, 3]

Use of call and apply:
1. Change this point

1 varObj1 = {2Name: "Obj1"3 };4 varObj2 = {5Name: "Obj2"6 };7 8window.name = "Window";9 Ten varGetName =function(){ OneConsole.log ( This. Name); A } -  -GetName ();//window theGetname.call (OBJ1);//obj1 -Getname.call (OBJ2);//Obj2

When executing the code for Getname.call (OBJ1), this in the GetName function points to the Obj1 object, so the

var function () {    this. name);}

is actually equivalent to

var function () {    console.log (obj1.name);}

2. Used to simulate the Function.prototype.bind of the specified function inside the

3. Using methods from other objects, you can simulate the implementation of inheritance

1 varA =function(name) {2      This. Name =name;3 }4 varB =function(){5A.apply ( This, arguments);6 }7B.prototype.getname =function(){8     return  This. Name;9 }Ten  One varb =NewB ("2B pencil"); AConsole.log (B.getname ());//output: 2B pencil

Use the method on the Array.prototype object to arguments the array object to the parameter list and call the Array object method.

1 (function() {2     Array.prototype.push.call (arguments, 3); 3     // output: [1, 2, 3] 4 }) (1, 2);

The bind () method in ECMAScript 5 can bind a function to an object

1 function f (y) {returnthis. x + y}; 2 var o = {x:1}; 3 var g = f.bind (o); 4 // 3

Js:this,call,apply,bind Summary

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.