本文摘自PHP中文网,作者coldplay.xixi,侵删。
最近又遇到了JacvaScript中的call()方法和apply()方法,而在某些时候这两个方法还确实是十分重要的,那么就让我总结这两个方法的使用和区别吧。
每个函数都包含两个非继承而来的方法:call()方法和apply()方法。
相同点:这两个方法的作用是一样的。
都是在特定的作用域中调用函数,等于设置函数体内this对象的值,以扩充函数赖以运行的作用域。
一般来说,this总是指向调用某个方法的对象,但是使用call()和apply()方法时,就会改变this的指向。
call()
方法使用示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <script>
window.color = 'red' ;
document.color = 'yellow' ;
var s1 = {color: 'blue' };
function changeColor(){
console.log( this .color);
}
changeColor.call();
changeColor.call(window);
changeColor.call(document);
changeColor.call( this );
changeColor.call(s1);
</script>
var Pet = {
words : '...' ,
speak : function (say) {
console.log(say + '' + this .words)
}
}
Pet.speak( 'Speak' );
var Dog = {
words: 'Wang'
}
Pet.speak.call(Dog, 'Speak' );
|
apply()
方法使用示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <script>
window.number = 'one' ;
document.number = 'two' ;
var s1 = {number: 'three' };
function changeColor(){
console.log( this .number);
}
changeColor.apply();
changeColor.apply(window);
changeColor.apply(document);
changeColor.apply( this );
changeColor.apply(s1);
</script>
function Pet(words){
this .words = words;
this .speak = function () {
console.log( this .words)
}
}
function Dog(words){
Pet.apply( this , arguments);
}
var dog = new Dog( 'Wang' );
dog.speak();
|
不同点:接收参数的方式不同。
apply()方法 接收两个参数,一个是函数运行的作用域(this),另一个是参数数组。
语法:apply([thisObj [,argArray] ]);,调用一个对象的一个方法,2另一个对象替换当前对象。
说明:如果argArray不是一个有效数组或不是arguments对象,那么将导致一个
TypeError,如果没有提供argArray和thisObj任何一个参数,那么Global对象将用作thisObj。
call()方法 第一个参数和apply()方法的一样,但是传递给函数的参数必须列举出来。
语法:call([thisObject[,arg1 [,arg2 [,...,argn]]]]);,应用某一对象的一个方法,用另一个对象替换当前对象。
说明: call方法可以用来代替另一个对象调用一个方法,call方法可以将一个函数的对象上下文从初始的上下文改变为thisObj指定的新对象,如果没有提供thisObj参数,那么Global对象被用于thisObj。
使用示例1:
1 2 3 4 5 6 7 | function add(c,d){
return this .a + this .b + c + d;
}
var s = {a:1, b:2};
console.log(add.call(s,3,4));
console.log(add.apply(s,[5,6]));
|
使用示例2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <script>
window.firstName = "Cynthia" ;
window.lastName = "_xie" ;
var myObject = {firstName: 'my' , lastName: 'Object' };
function getName(){
console.log( this .firstName + this .lastName);
}
function getMessage(sex,age){
console.log( this .firstName + this .lastName + " 性别: " + sex + " age: " + age );
}
getName.call(window);
getName.call(myObject);
getName.apply(window);
getName.apply(myObject);
getMessage.call(window, "女" ,21);
getMessage.apply(window,[ "女" ,21]);
getMessage.call(myObject, "未知" ,22);
getMessage.apply(myObject,[ "未知" ,22]);
</script>
|
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关学习推荐:javascript视频教程
以上就是实例汇总JS call()及apply()的方法使用的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
js中的箭头函数和普通函数有什么区别
详细介绍js中的变量提升机制
javascript和js之间有区别吗
手把手教你如何实现前端的吸顶效果
js函数的回调
js的dom是什么?
js事件之自建函数bind()与兼容性问题解决
js如何实现盒子拖拽效果?(附代码)
js如何实现页面跳转
js如何实现利用键盘控制div移动
更多相关阅读请进入《js》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 实例汇总JS call()及apply()的方法使用