JavaScript調(diào)用模式與this關(guān)鍵字綁定的關(guān)系
Invocation 調(diào)用
調(diào)用一個(gè)函數(shù)將暫停當(dāng)前函數(shù)的執(zhí)行,傳遞控制權(quán)和參數(shù)給新函數(shù)。
實(shí)參與形參不一致不會導(dǎo)致運(yùn)行時(shí)錯(cuò)誤,多的被忽略,少的補(bǔ)為undefined
每個(gè)方法都會收到兩個(gè)附加參數(shù):this和arguments。this的值取決于調(diào)用的模式,調(diào)用模式:方法,函數(shù),構(gòu)造器和apply調(diào)用模式
this被賦值發(fā)生在被調(diào)用的時(shí)刻。不同的調(diào)用模式可以用call方法實(shí)現(xiàn)
var myObject = {
value: 0,
increment: function (inc) {
this.value += typeof inc === 'number' ? inc : 1;
}
};
myObject.double = function(){
var helper = function(){
console.log(this);// this point to window
}
console.log(this);// this point to object myObject
helper();
}
myObject.double();//myObject Window
1 The Method Invocation Pattern 方法調(diào)用模式
方法:函數(shù)被保存為對象的屬性.當(dāng)方法被調(diào)用時(shí),this被綁定到該對象
公共方法:通過this取得他們所屬對象的上下文的方法
myObject.increment(); document.writeln(myObject.value); //
底層實(shí)現(xiàn): myObject.increment。call(myObject,0);
2 The Function Invocation Pattern 函數(shù)調(diào)用模式
當(dāng)函數(shù)并非對象的屬性時(shí)就被當(dāng)作函數(shù)調(diào)用(有點(diǎn)廢話。。),this被綁定到全局對象(window)
ECMAScript5中新增strict mode, 在這種模式中,為了盡早的暴露出問題,方便調(diào)試。this被綁定為undefined
var add = function (a,b) { return a + b;};
var sum = add(3,4);// sum的值為7
底層實(shí)現(xiàn):add.call(window,3,4)
strict mode:add.call(undefined,3,4)
方法調(diào)用模式和函數(shù)調(diào)用模式的區(qū)別
function hello(thing) {
console.log(this + " says hello " + thing);
}
person = { name: "Brendan Eich" }
person.hello = hello;
person.hello("world") // [object Object] says hello world 等價(jià)于 person。hello。call(person,“world”)
hello("world") // "[object DOMWindow]world" 等價(jià)于 hello。call(window,“world”)
3 The Constructor Invocation Pattern
JavaScript是基于原型繼承的語言,同時(shí)提供了一套基于類的語言的對象構(gòu)建語法。
this指向new返回的對象
var Quo = function (string) {
this.status = string;
};
Quo.prototype.get_status = function ( ) {
return this.status;
};
var myQuo = new Quo("this is new quo"); //new容易漏寫,有更優(yōu)替換
myQuo.get_status( );// this is new quo
4 The Apply Invocation Pattern
apply和call是javascript的內(nèi)置參數(shù),都是立刻將this綁定到函數(shù),前者參數(shù)是數(shù)組,后者要一個(gè)個(gè)的傳遞apply也是由call底層實(shí)現(xiàn)的
apply(this,arguments[]);
call(this,arg1,arg2...);
var person = {
name: "James Smith",
hello: function(thing,thing2) {
console.log(this.name + " says hello " + thing + thing2);
}
}
person.hello.call({ name: "Jim Smith" },"world","!"); // output: "Jim Smith says hello world!"
var args = ["world","!"];
person.hello.apply({ name: "Jim Smith" },args); // output: "Jim Smith says hello world!"
相對的,bind函數(shù)將綁定this到函數(shù)和調(diào)用函數(shù)分離開來,使得函數(shù)可以在一個(gè)特定的上下文中調(diào)用,尤其是事件bind的apply實(shí)現(xiàn)
Function.prototype.bind = function(ctx){
var fn = this; //fn是綁定的function
return function(){
fn.apply(ctx, arguments);
};
};
bind用于事件中
function MyObject(element) {
this.elm = element;
element.addEventListener('click', this.onClick.bind(this), false);
};
//this對象指向的是MyObject的實(shí)例
MyObject.prototype.onClick = function(e) {
var t=this; //do something with [t]...
};
總結(jié)
以上所述是小編給大家介紹的JavaScript調(diào)用模式與this關(guān)鍵字綁定的關(guān)系 ,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時(shí)回復(fù)大家的!
- 一文全面解析JS中的this綁定規(guī)則
- JavaScript中this綁定規(guī)則你理解了嗎
- 細(xì)說JavaScript中的this指向與綁定規(guī)則
- JavaScript this綁定與this指向問題的解析
- JavaScript?中的?this?綁定規(guī)則詳解
- JavaScript中this的綁定你知道幾種?
- 詳解JavaScript中的this硬綁定
- 一文搞懂JavaScript中的this綁定規(guī)則
- JavaScript中?this?的綁定指向規(guī)則
- 詳解JavaScript的this指向和綁定
- JavaScript this綁定過程深入詳解
- React.js綁定this的5種方法(小結(jié))
- 深入理解JavaScript this綁定規(guī)則
相關(guān)文章
使用Bootstrap美化按鈕實(shí)例代碼(demo)
這篇文章主要介紹了使用Bootstrap美化按鈕實(shí)例代碼(demo),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
JS添加或修改控件的樣式(Class)實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狫S添加或修改控件的樣式(Class)實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
Javascript中async與await的捕捉錯(cuò)誤詳解
這篇文章主要為大家詳細(xì)介紹了Javascript中async與await的捕捉錯(cuò)誤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
Javascript Function.prototype.bind詳細(xì)分析
這篇文章主要介紹了Javascript Function.prototype.bind詳細(xì)分析的相關(guān)資料,需要的朋友可以參考下2016-12-12

