js中call、apply、bind函式
1、call
var foo = {
user: "liyongfen",
getUser: function(){
console.log(this.user);
}
}
var bar = foo.getUser;
bar();//undefine
foo.getUser();//liyongfen
this沒有指向foo中的user
var foo = {
user: "liyongfen",
getUser: function(a,b){
console.log(a+b);
console.log(this.user);
}
}
var bar = foo.getUser;
bar.call(foo,1,3);//4 liyongfen
foo.getUser(5,6);//11 liyongfen
call第一個引數改變this的指向,後面的引數是對應getUser中引數
2、apply
var foo = {
user: "liyongfen",
getUser: function(a,b){
console.log(a+b);
console.log(this.user);
}
}
var bar = foo.getUser;
bar.call(foo,[1,3]);//4 liyongfen
foo.getUser(5,6);//11 liyongfen
apply裡的第一個引數指定this的指向,第二個引數是陣列。當第一個引數為null時,this指向window,call也一樣
3、bind
var foo = {
user: "liyongfen",
getUser: function(a,b){
console.log(this);
console.log(a+b);
console.log(this.user);
}
}
var bar = foo.getUser;
var c = bar.bind(foo);
c(4,8);//foo函式 12 liyongfen
bind繫結this後沒有立即執行,到想要執行是,再呼叫
相關文章
- 理解JS函式之call,apply,bindJS函式APP
- 手寫JS函式的call、apply、bindJS函式APP
- JS中的call、apply、bindJSAPP
- 理解JS中的call、apply、bind方法(********************************************************JSAPP
- js中call、apply、bind的區別JSAPP
- js call、apply、bind的實現JSAPP
- this、apply、call、bindAPP
- JavaScript—call, apply, bind 函式能幹啥?(全)(20)JavaScriptAPP函式
- 手寫系列:call、apply、bind、函式柯里化APP函式
- 重寫JS中的apply,call,bind,new方法JSAPP
- JavaScript 中的 apply、call、bindJavaScriptAPP
- 深入 call、apply、bind、箭頭函式以及柯里化APP函式
- Javascript - apply、call、bindJavaScriptAPP
- this, call, apply 和 bindAPP
- js深入之實現call、apply和bindJSAPP
- 模擬js中的call、apply和bind的實現JSAPP
- 手寫call,apply,bindAPP
- apply,call,bind的用法APP
- call、apply、bind 區別APP
- apply call bind 簡介APP
- call apply bind區別APP
- apply & call & bind 原始碼APP原始碼
- bind/call/apply 深度理解APP
- JavaScript-apply、bind、callJavaScriptAPP
- this指向與call,apply,bindAPP
- 手寫call、apply、bindAPP
- JS中改變this的指向 call、apply 和 bind 的區別JSAPP
- JavaScript重識bind、call、applyJavaScriptAPP
- [譯] Javascript: call()、apply() 和 bind()JavaScriptAPP
- JavaScript中call,apply,bind方法的總結。JavaScriptAPP
- 淺談JavaScript中的apply、call和bindJavaScriptAPP
- 談談JavaScript中的call、apply和bindJavaScriptAPP
- JS基礎總結(4)——this指向及call/apply/bindJSAPP
- js之call,apply和bind的模擬實現JSAPP
- js中call,apply和bind方法的區別和使用場景JSAPP
- 從一行等式理解JS當中的call, apply和bindJSAPP
- 函式中的apply,call入門介紹函式APP
- JavaScript進階教程(4)-函式內this指向解惑call(),apply(),bind()的區別JavaScript函式APP
- javascript中call()、apply()、bind()的用法終於理解JavaScriptAPP