當(dāng)構(gòu)造函數(shù)遇到 YUI3 封裝機(jī)制
來源:程序員人生 發(fā)布時(shí)間:2014-02-01 22:54:38 閱讀次數(shù):4265次
/*
* 構(gòu)造確認(rèn)操作對(duì)話框
* @constructor ConfirmBox
*/
var ConfirmBox, confirmOff, confirmDel;
ConfirmBox = function(msg, url){
this.msg = msg;
this.url = url;
};
ConfirmBox.prototype.fire = function(e){
var message = confirm(this.msg);
if(message){
window.location.href = this.url;
}
};
/*
* 確定下架此樓盤?
* @instance confirmOff
*/
confirmOff = new ConfirmBox('確定下架此樓盤?', config.offUrl);
if(Y.all('.sale-off')){
Y.on('click', confirmOff.fire, '.sale-off', confirmOff);
}
由于 YUI3 的封裝機(jī)制,通過 Y.on 注冊(cè)事件默認(rèn)上下文環(huán)境都是 Y 對(duì)象。而在 JavaScript 構(gòu)造函數(shù)中,其原型方法調(diào)用 this 時(shí)指向的是實(shí)例化的對(duì)象本身,如果不將 context 重置為實(shí)例本身,就掛了。如果沒重置,console.log this.msg 屬性就是下面這個(gè)情況鳥:
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)