JavaScript中call,apply,bind以及this,你了解多少?

说明

在JavaScript中调用一个函数将暂停当前函数的执行,传递控制权和参数给新函数。除了声明时定义形参,每一个函数接收两个附加的参数:this和arguments。参数this在面向对象中很是重要,它取决于调用的模式。在JavaScript中共有四种**调用模式:**方法调用模式、函数调用模式、构造器调用模式、和apply(),call()方法调用模式。这些模式在如何初始化关键参数this存在差别。本文首先要提到的是this,抛开this单独去说这些方法是没有意义的。而后是如何妙用call,apply,bind这些方法去改变this的指向。javascript

目录

this在不一样模式下的意义; 借鸡下蛋之妙用call,apply; 深刻理解bind函数; this在不一样模式下的意义:java

  1. 全局上下文 在全局运行上下文中(在任何函数体外部),this 指代全局对象,不管是否在严格模式下。例如在浏览器环境中任何定义在全局的属性,方法都将成为全局对象window的属性和方法。
console.log(this.document === document); // true
// 在浏览器中,全局对象为 window 对象:
console.log(this === window); // true
this.a = 37;
console.log(window.a); // 37
复制代码
  1. 函数上下文 在函数内部,this的值取决于函数是如何调用的。
//直接调用
function f1(){
  return this;
}
f1() === window; // true
//this的值不是由函数调用设定。由于代码不是在严格模式下执行,this 的值老是一个对象且默认为全局对象
function f2(){
 "use strict"; // 这里是严格模式
  return this;
}
f2() === undefined; // true
//在严格模式下,this 是在进入运行环境时设置的。若没有定义,this的值将维持undefined状态。也可能设置成任意值。
复制代码
  1. 对象方法中的this 当以对象里的方法的方式调用函数时,它们的 this 是调用该函数的对象. 下面的例子中,当 o.f() 被调用时,函数内的this将绑定到o对象。
var o = {
  prop: 38,
  f: function() {
    return this.prop;
  }
};
console.log(o.f()); // logs 38
复制代码

注意,在何处或者如何定义调用函数彻底不会影响到this的行为。在上一个例子中,咱们在定义o的时候为其成员f定义了一个匿名函数。可是,咱们也能够首先定义函数而后再将其附属到o.f。这样作this的行为也一致:数组

var o = {prop: 37};
function independent() {
  return this.prop;
}
o.f = independent;
console.log(o.f()); // logs 37
复制代码

这说明this的值只与函数 f 做为 o 的成员被调用有关系。 相似的,this 的绑定只受最靠近的成员引用的影响。在下面的这个例子中,咱们把一个方法g看成对象o.b的函数调用。在此次执行期间,函数中的this将指向o.b。事实上,这与对象自己的成员没有多大关系,最靠近的引用才是最重要的。浏览器

o.b = {
  g: independent,
  prop: 42
};
console.log(o.b.g()); // logs 42
复制代码
  1. 原型链中的this 相同的概念在定义在原型链中的方法也是一致的。若是该方法存在于一个对象的原型链上,那么this指向的是调用这个方法的对象,表现得好像是这个方法就存在于这个对象上同样。
var o = {
  f : function(){ 
    return this.a + this.b; 
  }
};
var p = Object.create(o);
p.a = 1;
p.b = 4;
console.log(p.f()); // 5
复制代码

在这个例子中,对象p没有属于它本身的f属性,它的f属性继承自它的原型。可是这对于最终在o中找到f属性的查找过程来讲没有关系;查找过程首先从p.f的引用开始,因此函数中的this指向p。也就是说,由于f是做为p的方法调用的,因此它的this指向了p。这是JavaScript的原型继承中的一个有趣的特性。 5. getter 与 setter 中的 this 再次,相同的概念也适用时的函数做为一个 getter 或者 一个setter调用。做为getter或setter函数都会绑定 this 到从设置属性或获得属性的那个对象。闭包

function modulus(){
  return Math.sqrt(this.re * this.re + this.im * this.im);
}
var o = {
  re: 1,
  im: -1,
  get phase(){
    return Math.atan2(this.im, this.re);
  }
};
Object.defineProperty(o, 'modulus', {
  get: modulus, enumerable:true, configurable:true});
console.log(o.phase, o.modulus); // logs -0.78 1.4142
复制代码
  1. 构造函数中的 this 当一个函数被做为一个构造函数来使用(使用new关键字),它的this与即将被建立的新对象绑定。 注意:当构造器返回的默认值是一个this引用的对象时,能够手动设置返回其余的对象,若是返回值不是一个对象,返回this。
function C(){
  this.a = 37;
}
var o = new C();
console.log(o.a); // logs 37
function C2(){
  this.a = 37;
  return {a:38};
}
o = new C2();
console.log(o.a); // logs 38
复制代码
  1. DOM事件处理函数中的 this 当函数被用做事件处理函数时,它的this指向触发事件的元素(一些浏览器在动态添加监听器时不遵照这个约定,除非使用addEventListener )。
// 被调用时,将关联的元素变成蓝色
function bluify(e){
  console.log(this === e.currentTarget); // 老是 true
  // 当 currentTarget 和 target 是同一个对象是为 true
  console.log(this === e.target);        
  this.style.backgroundColor = '#A5D9F3';
}
// 获取文档中的全部元素的列表
var elements = document.getElementsByTagName('*');
// 将bluify做为元素的点击监听函数,当元素被点击时,就会变成蓝色
for(var i=0 ; i<elements.length ; i++){
  elements[i].addEventListener('click', bluify, false);
}
复制代码

借鸡下蛋之妙用call,apply: apply fun.apply(thisArg[, argsArray])方法在指定 this 值和参数(参数以数组或类数组对象的形式存在)的状况下调用某个函数。app

  • thisArg 在 fun 函数运行时指定的 this 值。须要注意的是,指定的 this 值并不必定是该函数执行时真正的 this 值,若是这个函数处于非严格模式下,则指定为 null 或 undefined 时会自动指向全局对象(浏览器中就是window对象),同时值为原始值(数字,字符串,布尔值)的 this 会指向该原始值的自动包装对象。
  • argsArray 一个数组或者类数组对象,其中的数组元素将做为单独的参数传给 fun 函数。若是该参数的值为null 或 undefined,则表示不须要传入任何参数

在调用一个存在的函数时,你能够为其指定一个 this 对象。 this指当前对象,也就是正在调用这个函数的对象。使用apply,你能够只写一次这个方法而后在另外一个对象中继承它,而不用在新对象中重复写该方法。apply 与 call() 很是类似,不一样之处在于提供参数的方式。apply 使用参数数组而不是一组参数列表。apply 可使用数组字面量.你也可使用 arguments 对象做为 argsArray 参数。arguments 是一个函数的局部变量。 它能够被用做被调用对象的全部未指定的参数。 这样,你在使用apply函数的时候就不须要知道被调用对象的全部参数。 你可使用arguments来把全部的参数传递给被调用对象。 被调用对象接下来就负责处理这些参数。dom

  1. 使用apply来连接构造器
Function.prototype.construct = function (aArgs) {
  var oNew = Object.create(this.prototype);
  this.apply(oNew, aArgs);
  return oNew;
};
//另外一种可选的方法是使用闭包
Function.prototype.construct = function(aArgs) {
  var fConstructor = this, fNewConstr = function() { 
    fConstructor.apply(this, aArgs); 
  };
  fNewConstr.prototype = fConstructor.prototype;
  return new fNewConstr();
};
复制代码
  1. 使用call方法调用匿名函数
var animals = [
  {species: 'Lion', name: 'King'},
  {species: 'Whale', name: 'Fail'}
];
for (var i = 0; i < animals.length; i++) {
  (function (i) { 
    this.print = function () { 
      console.log('#' + i  + ' ' + this.species + ': ' + this.name); 
    } 
    this.print();
  }).call(animals[i], i);
}
复制代码
  1. 使用call方法调用匿名函数而且指定上下文的'this'
function greet() {
  var reply = [this.person, 'Is An Awesome', this.role].join(' ');
  console.log(reply);
}
var i = {
  person: 'Douglas Crockford', role: 'Javascript Developer'
};
greet.call(i); // Douglas Crockford Is An Awesome Javascript Developer
当一个函数的函数体中使用了this关键字时,经过全部函数都从Function对象的原型中继承的call()方法和apply()方法调用时,它的值能够绑定到一个指定的对象上。

 function add(c, d){
   return this.a + this.b + c + d;
 }
 var o = {a:1, b:3};
 // The first parameter is the object to use as 'this', subsequent parameters are passed as 
 // arguments in the function call
 add.call(o, 5, 7); // 1 + 3 + 5 + 7 = 16
 // The first parameter is the object to use as 'this', the second is an array whose
 // members are used as the arguments in the function call
 add.apply(o, [10, 20]); // 1 + 3 + 10 + 20 = 34
复制代码

使用 call 和 apply 函数的时候要注意,若是传递的 this 值不是一个对象,JavaScript 将会尝试使用内部 ToObject 操做将其转换为对象。所以,若是传递的值是一个原始值好比 7 或 'foo' ,那么就会使用相关构造函数将它转换为对象,因此原始值 7 经过new Number(7)被转换为对象,而字符串'foo'使用 new String('foo') 转化为对象,例如函数

function bar() {
   console.log(Object.prototype.toString.call(this));
 }
 bar.call(7); // [object Number]
复制代码

bind函数 fun.bind(thisArg[, arg1[, arg2[, ...]]])bind() 函数会建立一个新函数(称为绑定函数),新函数与被调函数(绑定函数的目标函数)具备相同的函数体(在 ECMAScript 5 规范中内置的call属性)。当目标函数被调用时 this 值绑定到 bind() 的第一个参数,该参数不能被重写。绑定函数被调用时,bind() 也接受预设的参数提供给原函数。一个绑定函数也能使用new操做符建立对象:这种行为就像把原函数当成构造器。提供的 this 值被忽略,同时调用时的参数被提供给模拟函数。ui

  1. 建立绑定函数 bind() 最简单的用法是建立一个函数,使这个函数不论怎么调用都有一样的 this 值。JavaScript新手常常犯的一个错误是将一个方法从对象中拿出来,而后再调用,但愿方法中的 this 是原来的对象。(好比在回调中传入这个方法。)若是不作特殊处理的话,通常会丢失原来的对象。从原来的函数和原来的对象建立一个绑定函数,则能很漂亮地解决这个问题:
his.x = 9; 
var module = {
  x: 81,
  getX: function() { return this.x; }
};
module.getX(); // 81
var retrieveX = module.getX;
retrieveX(); // 9, because in this case, "this" refers to the global object
// Create a new function with 'this' bound to module
//New programmers (like myself) might confuse the global var getX with module's property getX
var boundGetX = retrieveX.bind(module);
boundGetX(); // 81
复制代码
  1. 偏函数(Partial Functions) bind()的另外一个最简单的用法是使一个函数拥有预设的初始参数。这些参数(若是有的话)做为bind()的第二个参数跟在this(或其余对象)后面,以后它们会被插入到目标函数的参数列表的开始位置,传递给绑定函数的参数会跟在它们的后面。
function list() {
  return Array.prototype.slice.call(arguments);
}
var list1 = list(1, 2, 3); // [1, 2, 3]
// Create a function with a preset leading argument
var leadingThirtysevenList = list.bind(undefined, 37);
var list2 = leadingThirtysevenList(); // [37]
var list3 = leadingThirtysevenList(1, 2, 3); // [37, 1, 2, 3]
复制代码
  1. 配合 setTimeout 在默认状况下,使用 window.setTimeout() 时,this 关键字会指向 window (或全局)对象。当使用类的方法时,须要 this 引用类的实例,你可能须要显式地把 this 绑定到回调函数以便继续使用实例。
function LateBloomer() {
  this.petalCount = Math.ceil(Math.random() * 12) + 1;
}
// Declare bloom after a delay of 1 second
LateBloomer.prototype.bloom = function() {
  window.setTimeout(this.declare.bind(this), 1000);
};
LateBloomer.prototype.declare = function() {
  console.log('I am a beautiful flower with ' +
    this.petalCount + ' petals!');
};
var flower = new LateBloomer();
flower.bloom();  // 一秒钟后, 调用'declare'方法
复制代码
  1. 快捷调用 在你想要为一个须要特定的 this 值得函数建立一个捷径(shortcut)的时候,bind() 方法也很好用.你能够用 Array.prototype.slice 来将一个相似于数组的对象(array-like object)转换成一个真正的数组,就拿它来举例子吧。你能够建立这样一个捷径:
var slice = Array.prototype.slice;
// ...
slice.apply(arguments);
复制代码

用 bind() 可使这个过程变得简单。在下面这段代码里面,slice 是 Function.prototype 的 call() 方法的绑定函数,而且将 Array.prototype 的 slice() 方法做为 this 的值。这意味着咱们压根儿用不着上面那个 apply() 调用了。this

// same as "slice" in the previous example
var unboundSlice = Array.prototype.slice;
var slice = Function.prototype.call.bind(unboundSlice);
// ...
slice(arguments);
复制代码

ECMAScript 5 引入了 Function.prototype.bind。调用f.bind(someObject)会建立一个与f具备相同函数体和做用域的函数,可是在这个新函数中,this将永久地被绑定到了bind的第一个参数,不管这个函数是如何被调用的。

function f(){
  return this.a;
}
var g = f.bind({a:"azerty"});
console.log(g()); // azerty
var o = {a:37, f:f, g:g};
console.log(o.f(), o.g()); // 37, azerty
复制代码
相关文章
相关标签/搜索