js: this,call,apply,bind 总结
对js中的一些基本的很重要的概念做一些总结,对进一步学习js很重。
一、this
JavaScript 中的 this 总是指向一个对象,而具体指向那个对象是在运行时基于函数的执行环境动态绑定的,而非函数声明时的环境
实际应用中 this 的指向大致可以分为以下 4 中:
1. 作为对象的方法调用
2. 作为普通函数掉用
3. 构造器调用
4. Function.prototype.call 或 Function.prototype.apply 调用, 可以动态地改变出入函数的 this
1. 作为对象的方法调用时, this 指向该对象
var obj = {
a: 1,
getA: function(){
console.log( this == obj ); // true
console.log( this.a ); //
}
};
obj.getA();
2. 作为普通函数掉用,this 总是指向全局对象 window
console.log(this); // Windows window.name = "globalName";
var getName = function() {
return this.name;
} console.log( getName() ); // globalName
3. 构造器调用, 当用 new 运算符调用函数时,该函数总是会返回一个对象,通常情况下,构造函数里的 this 就指向返回的这个对象
var MyClass = function(){
this.name = "class";
}
var obj = new MyClass();
console.log( obj.name ); // class
如果使用 new 调用构造器时,构造器显式地返回了一个 object 类型的对象,那么此次运算结果最终会返回这个对象,而不是我么之前期待的 this
var MyClass = function(){
this.name = "class";
return {
name: "other"
}
}
var obj = new MyClass();
console.log(obj.name); // other
二、 call 和 apply
他们的作用一模一样,区别仅在于传入参数形式的不同。
apply 接收两个参数,第一个参数指定了函数体内 this 对象的指向,第二个参数为一个带下标的集合,这个集合可以是数组,也可以是类数组,apply 方法把这个集合中的元素作为参数传入被调用的函数。
call 传入的参数不固定,跟 apply 相同的是,第一个参数也代表函数体内的 this 指向,从第二个参数开始往后,每个参数被依次传入函数
var func = function(a, b, c){
console.log([a, b, c]);
}
//传入的第一个参数为 null ,函数体内的 this 会指向默认的宿主对象,在浏览器中则是 window
func.apply(null, [1, 2, 3]); // 输出:[ 1, 2, 3 ]
func.call(null, 1, 2, 3); // 输出:[ 1, 2, 3 ]
call 和 apply 的用途:
1. 改变 this 指向
var obj1 = {
name: "obj1"
};
var obj2 = {
name: "obj2"
};
window.name = "window";
var getName = function(){
console.log( this.name );
}
getName(); // window
getName.call( obj1 ); // obj1
getName.call( obj2 ); // obj2
当执行 getName.call( obj1 ) 这句代码时, getName 函数体内的 this 就指向 obj1 对象,所以此处的
var getName = function(){
console.log( this.name );
}
实际上相当于
var getName = function(){
console.log( obj1.name );
}
2. 用来模拟 Function.prototype.bind 指定函数内部的 this 指向
3. 借用其他对象的方法, 可以模拟实现继承
var A = function(name){
this.name = name;
}
var B = function(){
A.apply( this, arguments);
}
B.prototype.getName = function(){
return this.name;
}
var b = new B("2B铅笔");
console.log( b.getName() ); // 输出: 2B铅笔
借用 Array.prototype 对象上的方法,对参数列表 arguments 这个类数组对象,进行数组对象方法的调用
(function(){
Array.prototype.push.call( arguments, 3);
console.log( arguments ); // 输出: [1, 2, 3]
})(1, 2);
三、ECMAScript 5 中的 bind() 方法可以将函数绑定到一个对象上
function f(y) {return this.x + y};
var o = { x: 1};
var g = f.bind(o);
g(2); //
js: this,call,apply,bind 总结的更多相关文章
- JS 的 call apply bind 方法
js的call apply bind 方法都很常见,目的都是为了改变某个方法的执行环境(context) call call([thisObj[,arg1[, arg2[, [,.argN]]]] ...
- JS中call,apply,bind方法的总结
why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user: "小马扎", fn: ...
- 学习前端的菜鸡对JS的call,apply,bind的通俗易懂理解
call,apply,bind 在JavaScript中,call.apply和bind是Function对象自带的三个方法,都是为了改变函数体内部 this 的指向. a ...
- js 中call,apply,bind的区别
call.apply.bind方法的共同点与区别: apply.call.bind 三者都是用来改变函数的this对象的指向: apply.call.bind 三者都可以利用后续参数传参: bind ...
- 原生JS实现call,apply,bind函数
1. 前言 使用原生JS实现call和apply函数,充分了解其内部原理.call和apply都是为了解决改变this的指向.作用都相同,只是传参的方式不同.除了第一个参数外,call可以接受一个参数 ...
- js笔记——call,apply,bind使用笔记
call和apply obj.call(thisObj, arg1, arg2, ...); obj.apply(thisObj, [arg1, arg2, ...]); 两者作用一致,都是把obj( ...
- JS之call/apply/bind
测试代码: var a = 1; var obj = { a = 2; } function test(a){ alert(a); alert(this.a); } 1.test(3); 结果:3,1 ...
- js 对call apply bind理解
请参考 http://www.cnblogs.com/xljzlw/p/3775162.html 1.call和apply的区别:参数类型不同var mtt = { name: "mtt&q ...
- js new call apply bind 的 原理
new new 做了什么事?1. 以 Object.protoype 为原型创建一个新对象 2. 以新对象为 this,执行函数的 [[call]] 3. 如果 [[call]] 的返回值是对象,那么 ...
随机推荐
- on the way to Peking University
明天就要去北京参加北大夏令营了,希望这次能有所斩获! on the way to Peking University
- DataSet筛选数据然后添加到新的DataSet中引发的一系列血案
直入代码: var ds2 = new DataSet(); ) { ].Select(" usertype <> 'UU'"); ) { DataTable tmp ...
- [转] 计算几何模板Orz
#include<math.h> #define MAXN 1000 #define offset 10000 #define eps 1e-8 #define PI acos(-1.0) ...
- Linux/Unix System Level Attack、Privilege Escalation(undone)
目录 . How To Start A System Level Attack . Remote Access Attack . Local Access Attack . After Get Roo ...
- ubuntu 14.04 vim install youcompleteme
sudo apt-get install vim ; sudo apt-get install vim-youcompleteme ; sudo apt-get install vim-addon-m ...
- SOAP 格式设置选项
SOAP 格式设置选项 两个格式设置选项为: Style:适用于 SOAP 消息中 Body 元素的子元素(也可能是孙级).此选项指定为 binding WSDL 元素(通常情况下)或 operati ...
- C# Attribute 特性 学习
一.特性的概述 公共语言运行库允许您添加类似关键字的描述性声明(称为特性 (Attribute))来批注编程元素,如类型.字段.方法和属性 (Property).属性与 Microsoft .NET ...
- 旋转屏幕时,假如自定义的xib大小变了,可能是这个属性没有修改
虽然xib内部启用了自动布局,但是当xib放入外界,xib自身的autoresizing是存在的
- serialVersionUID要注意以下几点:
今天在使用eclipse开发的时候,遇到一个warning,看到warning我总觉得不爽,使用自动修复后,发现eclipse在代码中加入了“private static final long ser ...
- 使用spring集成hibernate
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...