第一部分 基础知识

第2章  this 、 call 和 apply

2.1  this

  JavaScript的 this 总是指向一个对象,而具体指向哪个对象是在运行时基于函数的执行环境动态绑定的,而非函数被声明时的环境。也就是说声明时不知道,运行时才知道。

   this 的指向大致可以分为以下 4种:

   作为对象的方法调用。

<script type="text/javascript">
//当函数作为对象的方法被调用时, this 指向该对象:
var obj = {
a:1,
getA:function(){
console.log(this === obj);
console.log(this.a);
}
}
obj.getA();
</script>

    作为普通函数调用。

<script>
//作为普通函数调用
window.name = 'globalName';
var getName = function(){
return this.name;
}
console.log(getName());
var getId = function( id ){
return document.getElementById( id );
};
getId( 'div1' );
</script>

    构造器调用。

<script>
var MyClass = function(){
this.name = 'sven';
};
var obj = new MyClass();
alert ( obj.name ); // 输出:sven
</script>

    Function.prototype.call 或 Function.prototype.apply 调用。

<script>
var obj1 = {
name: 'sven',
getName: function(){
return this.name;
}
};
var obj2 = {
name: 'anne'
};
console.log( obj1.getName() ); // 输出: sven
console.log( obj1.getName.call( obj2 ) ); // 输出:anne
</script>

2.2  call 和 apply

  ECAMScript 3给 Function 的原型定义了两个方法,它们是 Function.prototype.call 和 Function.prototype.apply 。

2.2.1  call 和 apply 的区别

  apply 接受两个参数,第一个参数指定了函数体内 this 对象的指向,第二个参数为一个带下标的集合,这个集合可以为数组,也可以为类数组, apply 方法把这个集合中的元素作为参数传递给被调用的函数。

2.2.2  call 和 apply 的用途

  1. 改变 this 指向

<script>
var obj1 = {
name: 'sven'
};
var obj2 = {
name: 'anne'
};
window.name = 'window';
var getName = function(){
alert ( this.name );
};
getName(); // 输出: window
getName.call( obj1 ); // 输出: sven
getName.call( obj2 ); // 输出: anne
</script>

  2.  Function.prototype.bind

	Function.prototype.bind = function(){
var self = this,// 保存原函数
context = [].shift.call(arguments);// 需要绑定的 this 上下文
args = [].slice.call(arguments);// 剩余的参数转成数组
// 返回一个新的函数
return function(){
// 执行新的函数的时候,会把之前传入的 context 当作新函数体内的 this
// 并且组合两次分别传入的参数,作为新函数的参数
return self.apply(context, [].concat.call(args, [].slice.call(arguments)));
}
} var obj1 = {
name:'john'
};
var func1 = function(a,b,c,d){
console.log(this.name);
console.log([a,b,c,d]);
}.bind(obj1, 1,2); func1(3,4);

  3. 借用其他对象的方法

    第一种场景是“借用构造函数”

//借用构造函数
var A = functiono(name){
this.name = name;
}
var B = function(){
A.apply(this, arguments);
} B.prototype.getName = function(){
return this.name;
} var b = new B('john');
console.log(b.getName());

    第二种场景借用 Array.prototype 对象上的方法

    //借用其它对象
(function(){
Array.prototype.push.call(arguments, 3);
console.log(arguments);
})(1,2);

最新文章

  1. div 加载 html页面的方法
  2. 一个Java递归删除目录的方法
  3. Maven生命周期(插件)
  4. 类的大小——sizeof 的研究
  5. Python requests模块
  6. cloudera manager 及CDH卸载
  7. 可变字典 NSMutableDictionary
  8. 分享一个在PearOS里面的plank的配置文件
  9. Django- &#39;WSGIRequest&#39; object has no attribute &#39;user&#39;
  10. 用《内网穿山甲》把本地IIS中的站点共享到远程访问
  11. arcgis for js开发之路径分析
  12. 生信分析常用脚本(二)--SOAPdenovo
  13. BZOJ1005 HNOI2008明明的烦恼(prufer+高精度)
  14. tensorflow学习之(四)使用placeholder 传入值
  15. error loading midas.dll问题
  16. ftp主动与被动模式区别
  17. node.js 开发桌面程序, 10个令人惊讶的NodeJS开源项目
  18. Action&lt;T&gt;和Func&lt;T&gt;委托
  19. xaml mvvm(2)之属性绑定
  20. 30 个免费的 Sketch 必备插件

热门文章

  1. 各个领域常见的一些bug汇总
  2. double x = 10 ,y = 0;y = x % 2; 这个表达式正确吗?
  3. 设计模式C++实现——外观模式
  4. Resources.Theme
  5. 把一个文件夹下的多个excel文件合并到同一个excel的一个sheet里
  6. 包教包会:本地推送 &amp; 远程推送
  7. 利用html sessionStorge 来保存局部页面在刷新后回显,保留
  8. [Apple开发者帐户帮助]二、管理你的团队(6)找到您的团队ID
  9. LocalDateTime查找最近的五分钟点
  10. python 7:del 列表指定元素、list.pop(索引)、list.remove(元素值)(删除列表指定元素,且不可再使用;默认索引-1,弹出指定列表元素,可再使用;移除列表指定第一个元素)