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
- JavaScript中bind、call、apply函式用法詳解JavaScriptAPP函式
- JS中的call、apply、bind方法JSAPP
- js中call、apply、bind的用法JSAPP
- 理解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函式
- JavaScript 中的 apply、call、bindJavaScriptAPP
- js call、apply、bind的實現JSAPP
- 重寫JS中的apply,call,bind,new方法JSAPP
- 深入 call、apply、bind、箭頭函式以及柯里化APP函式
- 回味JS基礎:call apply 與 bindJSAPP
- js call apply bind簡單的理解JSAPP
- Javascript - apply、call、bindJavaScriptAPP
- this, call, apply 和 bindAPP
- 模擬js中的call、apply和bind的實現JSAPP
- js深入之實現call、apply和bindJSAPP
- apply & call & bind 原始碼APP原始碼
- bind/call/apply 深度理解APP
- 手寫call,apply,bindAPP
- apply,call,bind的用法APP
- call、apply、bind 區別APP
- apply call bind 簡介APP
- 手寫call、apply、bindAPP
- this指向與call,apply,bindAPP
- call apply bind區別APP
- JavaScript-apply、bind、callJavaScriptAPP
- JS中改變this的指向 call、apply 和 bind 的區別JSAPP
- JS核心系列:淺談 call apply 與 bindJSAPP
- JavaScript中call,apply,bind方法的總結。JavaScriptAPP
- 談談JavaScript中的call、apply和bindJavaScriptAPP
- 淺談JavaScript中的apply、call和bindJavaScriptAPP
- JavaScript中call,apply,bind方法的總結JavaScriptAPP