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]] 的返回值是对象,那么 ...
随机推荐
- NOI题库--砝码称重V2(多重背包2^n拆分)
以前只会写多重背包的原版,渣的不行,为了做此题不得不学习了一下,发现其实也不难,只要理解了方法就好多了(PS:其实和倍增挺像的) 8756:砝码称重V2 总时间限制: 1000ms 内存限制: 655 ...
- Memory Allocation API In Linux Kernel && Linux Userspace、kmalloc vmalloc Difference、Kernel Large Section Memory Allocation
目录 . 内核态(ring0)内存申请和用户态(ring3)内存申请 . 内核态(ring0)内存申请:kmalloc/kfree.vmalloc/vfree . 用户态(ring3)内存申请:mal ...
- CustomerConfigHelper
public static class CustomerConfigHelper { public static object _lockObject = new object(); private ...
- MyEclipse------遍历某个路径下的(所有或特定)文件和目录
usebean包(自己定义的,在src文件夹下面)里的java文件 FileAccept.java package usebean; import java.io.File; import java. ...
- File类的创建,删除文件
File.Create(@"C:\Users\shuai\Desktop\new.txt"); Console.WriteLine("创建成功"); Conso ...
- 利用dedecms autoindex让文章列表加上序列号
有些时候我们在制作模板的需要在文章标题前面加上序列号,可以通过织梦自带的autoindex属性来实现,实现方法很简单,只需要在序号递增的地方加上 这段代码就行,[field:global runphp ...
- [Redis]c# redis缓存辅助类
public static class RedisCache { private static IRedisClient RCClient = null; /// <summary> // ...
- C语言运算符优先级和口诀 (转)
一共有十五个优先级: 1 () [] . -> 2 ! ~ -(负号) ++ -- &(取变量地址)* (type)(强制类型) sizeof 3 ...
- pdo调用
php单次调用,例题 <body> <?php //造DSN:驱动名:dbname=数据库名;host=服务器地址 $dsn = "mysql:dbname=mydb;ho ...
- 分页控件-ASP.NET(AspNetPager)
AspNetPager是asp.net中常用的分页控件,下载AspNetPager.dll,添加引用,在工具栏就可以看到AspNetPager控件: <div class="oa-el ...