转自--https://www.cnblogs.com/chenpingzhao/p/4883995.htmljavascript
咱们先来看一道题目html
var write = document.write; write("hello"); //1.以上代码有什么问题 //2.正确操做是怎样的
不能正确执行,由于write函数丢掉了上下文,此时this的指向global或window对象,致使执行时提示非法调用异常,因此咱们须要改变this的指向java
正确的方案就是使用 bind/call/apply来改变this指向
web
bind方法app
var write = document.write; write.bind(document)('hello');
call方法dom
var write = document.write; write.call(document,'hello');
apply方法函数
var write = document.write; write.apply(document,['hello']);
bind()
最简单的用法是建立一个函数,使这个函数不论怎么调用都有一样的this值。常见的错误就像上面的例子同样,将方法从对象中拿出来,而后调用,而且但愿this指向原来的对象。若是不作特殊处理,通常会丢失原来的对象。使用bind()
方法可以很漂亮的解决这个问题:this
<script type="text/javascript"> this.num = 9; var module = { num: 81, getNum: function(){ console.log(this.num); } }; module.getNum(); // 81 ,this->module var getNum = module.getNum; getNum(); // 9, this->window or global var boundGetNum = getNum.bind(module); boundGetNum(); // 81,this->module </script>
这是一个很好的特性,使用bind()
咱们设定函数的预约义参数,而后调用的时候传入其余参数便可:spa
<script type="text/javascript"> function list() { return Array.prototype.slice.call(arguments); } var list1 = list(1, 2, 3); console.log(list1);// [1, 2, 3] // 预约义参数37 var leadingThirtysevenList = list.bind(undefined, 37); var list2 = leadingThirtysevenList(); console.log(list2);// [37] var list3 = leadingThirtysevenList(1, 2, 3); console.log(list3);// [37, 1, 2, 3] </script>
通常状况下setTimeout()
的this指向window或global对象。当使用类的方法时须要this指向类实例,就可使用bind()
将this绑定到回调函数来管理实例。prototype
<script type="text/javascript"> function Bloomer() { this.petalCount = Math.ceil(Math.random() * 12) + 1; } // 1秒后调用declare函数 Bloomer.prototype.bloom = function() { window.setTimeout(this.declare.bind(this), 1000); }; Bloomer.prototype.declare = function() { console.log('我有 ' + this.petalCount + ' 朵花瓣!'); }; var test = new Bloomer(); test.bloom(); </script>
绑定函数也适用于使用new操做符来构造目标函数的实例。当使用绑定函数来构造实例,注意:this会被忽略,可是传入的参数仍然可用。
<script type="text/javascript"> function Point(x, y) { this.x = x; this.y = y; } Point.prototype.toString = function() { console.log(this.x + ',' + this.y); }; var p = new Point(1, 2); p.toString(); // 1,2 var YAxisPoint = Point.bind(null,10); var axisPoint = new YAxisPoint(5); axisPoint.toString(); // 10,5 console.log(axisPoint instanceof Point); // true console.log(axisPoint instanceof YAxisPoint); // true console.log(new Point(17, 42) instanceof YAxisPoint); // true </script>
上面例子中Point和YAxisPoint共享原型,所以使用instanceof运算符判断时为true