Javascript 中 apply() 和 call() 的区别
来源:互联网 发布:ipad钢琴教学软件 编辑:程序博客网 时间:2024/04/30 03:19
call() is a method of function object. It allows you to call (execute) a function (object) as if this function were a method of another object.
Syntax
fun.call(obj, arg1, arg2, ...);
Scope of this keyword
With the call(), the context of the function fun is executed as if it were the method of object obj. The keywordthis in the function fun will be the first argument in call() method obj. If the obj isnull or undefined, the value ofthis keyword will be the global object (Window object). The rest of the arguments starting from the 2nd one in call() method is the arguments which are needed to excecute the function fun.
The following examples help to understand the scope of this keyword.
var
name = "Mark";var
fun =function
(txt){ alert(txt +this
.name );};var
obj = {name: "Tom" };fun.call(obj, "Hello, "); // return Hello Tom coz this is objfun.call(null
, "Hello, "); // return Hello Mark coz this is the global object
The following code is equivalent to the 1st call above in that fun is defined as a method of obj - obj.me. It's natually to seethis keyword in fun is the object obj.
var
fun =function
(txt){ alert(txt +this
.name );};var
obj = { name: "Tom", me: fun};obj.me("Hello "); // return Hello Tom
Difference between call() and apply()
The apply() method is the similar as the call() method. The difference is that the argument in apply() is given in Array.
Typical Use
Use call() to chain constructors for an object.
function
div(width, height) {this
.width = width;this
.height = height; show.call(this
, width, height); // executing context changed from div to show, // so this changed from div to show }function
show() {this
.css =function
() { alert("{width: "+this
.width + "; height: "+this
.height+"}"); }; }var
div1 =new
div("20px","40px");div1.css(); // return {width: "20px"; height: "40px"}
Function bind method
One useful application is building the bind method to the prototype of the Function object. The bind method is shared and can be invoked by all functions through inheritance.
Function.prototype.bind =function
(obj) {var
_method =this
;return
function
() {return
_method.apply(obj, arguments); }; }
The benefit is that bind returns a function reference that can be used later, rather than the result of an immediate execution that we get with call. Using the bind method, the above code can be written as following,
function
div(width, height) {this
.width = width;this
.height = height;var
_show = show.bind(this
); _show(width, height);// show is the function to be executed rather than the function name// _show is the binded function reference of the function of show// with this keyword changed to div object }var
show =function
() {this
.css =function
() { alert("{width: "+this
.width + "; height: "+this
.height+"}"); }; }var
div1 =new
div("20px","40px");div1.css(); // return {width: "20px"; height: "40px"}本文来源:http://www.pagecolumn.com/javascript/apply_call.htm
- javascript 中apply 和call的区别
- Javascript 中 apply() 和 call() 的区别
- javascript中call()和apply()的区别
- javascript中apply()和call()的区别
- javascript 中 call() 和 apply() 的区别
- JavaScript中call和apply的区别
- javascript中call和apply的区别
- Javascript中call和apply的区别
- 关于javascript中apply()和call()方法的区别javascript
- javascript中apply和call区别
- 关于javascript中apply()和call()方法的区别
- 关于javascript中apply()和call()方法的区别
- JavaScript中apply(),call()的区别和关系
- JavaScript中apply和call的用法区别
- 关于javascript中apply()和call()方法的区别
- javascript中apply()和call()方法的区别
- 关于javascript中apply()和call()方法的区别
- 【js】javascript中call()和apply()方法的区别
- 复数类及各种操作(Java)
- 40款不容错过的个人摄影设计作品集网站
- Heat equation
- UIthread UI 线程
- 每天一个linux命令(15):tail 命令
- Javascript 中 apply() 和 call() 的区别
- 每天一个linux命令(16):which命令
- Access用OleDbParameter无法更新数据解决方案
- 家庭文件共享,FreeNas服务器的搭建过程
- 双卡手机,sim卡状态改变测试
- 使用CFindReplaceDialog
- Android 对Gallery的挖掘
- 第十一周上级任务--穷尽式列表借书问题
- 数据库数据被改,使用log explorder查询修改记录