this bind apply call
this 是当前函数运行时所属的对象
bind 是指定一个函数运行时的上下文,也就是说把这个函数的this指向绑定到相应对象上,默认的暴露在全局御中的函数this指向widow对象, 严格模式下全局的this为undefined
call和apply的作用是一样的 ,大致是执行某个函数但是用另一个对象的实例替换为上下文执行,call传参数的方法是通过" ,"号的方式分隔着传进去,
apply是直接传一个参数数组进去
call方法:
语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]])
定义:调用一个对象的一个方法,以另一个对象替换当前对象。
说明:
call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。
如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。
// 示例1:
var tmp = {
dd:20,
add:function (a,b){
console.log('tmp.add');console.log(this);
return a+b;
},
sub:function (a,b) {
return a-b;
}
}; var tmp2 = {
dd:30,
add:function (a,b) {
console.log('tmp2.add');console.log(this);
return a+b+5;
}
}; var res = tmp.add.call(tmp2,3,2);
console.log(res); // 5 this.dd:30 //即这里调用tmp.add方法,但是用tmp2对象来替换tmp的add方法执行时的this指向
//示例2:
function dog() {
this.name = '小黑';
this.showName = function (){
console.log(this.name);
return this.name;
}
} function cat() {
this.name = '小白';
} var aDog = new dog(); var res = aDog.showName.call(new cat() );
console.log(res); // 小白, 小白
通过这样,可以实现js的继承,可以通过prototype(原型来实现继承),也可以直接通过call方法来实现继承
function Animal(name){
this.name = name;
this.showName = function(){
alert(this.name);
}
}
function Cat(name){
Animal.call(this, name);
}
var cat = new Cat("Black Cat");
cat.showName();
在Cat对象内调用了call方法,从而使this内扩展了Animal从而可以调用animal的方法
也可以多次call 实现多重继承,但多次继承时,实例的this指向最后一行call的类
function Animal(name){
this.name = 'animal';
this.showName = function(){
console.log(this.name);
console.log(name);
}
console.log(arguments); //
}
function Mao(name) {
this.name = 'mao';
}
function Cat(name){
this.name = 'cat'
Animal.call(this, name);
Mao.call(this);
}
var cat = new Cat("Black Cat"); // 输出 arguments对象: ['Black Cat']
cat.showName(); // 输出 mao , Black Cat
apply方法和call方法作用一样,但由于apply方法会自动把一个数组作为一个一个的参数传入进去,所以,apply可以有一些更巧妙的用法
apply的特性应用如:
1. Math.max 可以实现得到数组中最大的一项
因为Math.max 参数里面不支持Math.max([param1,param2]) 也就是数组
但是它支持Math.max(param1,param2,param3…),所以可以根据刚才apply的那个特点来解决 var max=Math.max.apply(null,array),这样轻易的可以得到一个数组中最大的一项
2.Array.prototype.push 可以实现两个数组合并,同样push方法没有提供push一个数组,但是它提供了push(param1,param,…paramN) 所以同样也可以通过apply来装换一下这个数组,即
vararr1=new Array("1","2","3");
vararr2=new Array("4","5","6");
Array.prototype.push.apply(arr1,arr2);
this bind apply call的更多相关文章
- javascript中bind,apply,call的相同和不同之处
javasctipt中bind,apply,call的相同点是: 1,都是用来改变this的指向; 2,都可以通过后续参数进行传参; 3,第一个参数都是指定this要指向的对象; 不同点: 1,调用方 ...
- javascript 的bind/apply/call性能
javascript有两种使用频率非常高的三个内置的功能:bind/apply/call.许多技术是基于高点,这些功能实现.这三个功能被用来改变的功能运行环境.从而达到代码复用的目的. 先来所说bin ...
- .bind.apply() 解决 new 操作符不能用与 apply 或 call 同时使用
背景: 小明想要用数组的形式为 Cls.func 传入多个参数,他想到了以下的写法: var a = new Cls.func.apply(null, [1, 2, 3]); 然而浏览器却报错Cls. ...
- bind,apply,call的区别
在Javascript中,bind, apply, call方法都可以显式绑定上下文this,这三者有何不同呢? bind只绑定this不马上执行 var person = { firstname: ...
- 箭头函数表达式和声名式函数表达式的区别以及 Function.prototype的bind, apply,call方法
箭头函数不能用做构造函数 箭头函数没有arguments参数 箭头函数没有自己的this,是从作用域链上取this,是与箭头函数定义的位置有关的,与执行时谁调用无关,所以用call,apply,bin ...
- JavaScript: bind apply call
var foo = function(age,sex){ console.log(this.name,age,sex); }; //call将改变函数运行的context foo.call({name ...
- bind,apply,call区别总结
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- call bind apply的区别
call() 和apply()的第一个参数相同,就是指定的对象.这个对象就是该函数的执行上下文. call()和apply()的区别就在于,两者之间的参数. call()在第一个参数之后的 后续所有参 ...
- 数组去重,call、apply、bind之间的区别,this用法总结
一.数组去重,直接写到Array原型链上. //该方法只能去除相同的数字 不会去判断24和'24'是不同的 所有数字和字符串数字是相同是重复的 Array.prototype.redup=functi ...
随机推荐
- Numpy 索引及切片
1.一维数组的索引及切片 ar = np.arange(20) print(ar) print(ar[4]) print(ar[3:6]) print(ar[:4:2]) #索引到4 按2的步长 pr ...
- hasattr() & getattr() & setattr()
Python的hasattr() getattr() setattr() 函数使用方法详解 感谢作者 ---> 原文链接 hasattr(object, name) 判断一个对象里面是否有n ...
- 笔记-selenium+chrome headless
笔记-selenium+chrome headless 1. selenium+chrome headless phantomjs与selenium分手了,建议使用其它无头浏览器. chro ...
- Oozie 之 sqoop 实战
1.创建 lib 目录并拷贝 mysql 支持包 2.修改 job.properties 文件 nameNode=hdfs://cen-ubuntu.cenzhongman.com:8020 jobT ...
- 如何使用Python脚本
来自官方文档 一.写 python 脚本: import sys import datetime for line in sys.stdin: line = line.strip() userid, ...
- SpringMVC---applicationContext.xml配置详解
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 51Nod 1680 区间求和 树状数组
题意: 给出一个长度为\(n\)的数列\(A_i\),定义\(f(k)\)为所有长度大于等于\(k\)的子区间中前\(k\)大数之和的和. 求\(\sum_{k=1}^{n}f(k) \; mod \ ...
- Android 打印方法调用堆栈
RuntimeException here = new RuntimeException("here"); here.fillInStackTrace(); Log.w(" ...
- 《Cracking the Coding Interview》——第17章:普通题——题目3
2014-04-28 22:18 题目:计算N的阶乘尾巴上有多少个零? 解法:计算5的个数即可,因为2 * 5 = 10,2的个数肯定比5多.计算5的个数可以在对数时间内搞定. 代码: // 17.3 ...
- 求:斐波那契数列的第n项
def he (n): if n < 3 : return 1 return he(n-1)+he(n-2)print(he(n))