我想一個參數綁定的this
一個名爲Sequence
結合作品的功能,問題是超過一個Sequence
覆蓋對方,所以我必須用new
這裏的問題...用這個新的構造函數綁定一個參數?
//js code
function init(cmd) {
cmd.exec.call(e,Sequence.bind(cmd));
}
例如
init({
exec:function(seq){
seq("a",function(){
console.log(this);// returns init object itself
});
}
});
偉大的作品,但是當我做
//init for js above
...,function(seq) {
seq("a",function(){
console.log ("hello");
},document.getElementById("google"));
});
...,function(seq) {
seq("d",function(){
console.log("goodbye");
});
});
第二個序列運行goodbye
。從來沒有第一個,因爲它被寫了。
序列功能
function Sequence(key, fn, location) {
if (!location) location = document;
var self = this; //object that is bound to Sequence
location.addEventListener("keydown", function sequenceMode(e) {
if(self.waiting)
{
if (keyCodes.literal[key.toUpperCase()] === e.which) {
fn.call(self,e);
self.waiting = false;
this.removeEventListener("keydown", sequenceMode);
}
} else location.removeEventListener("keydown", sequenceMode);
});
}
所以在這裏我的問題是我怎麼A
了這個屬性綁定爲對象調用Sequence
或B
我怎麼創建的Sequence
一個新的實例,並仍允許用戶在函數內部定義?
cmd.call(e,new Sequence().bind(cmd)); //can not call bind from Constructor
所以基本上我需要的用戶仍然能夠定義參數本身的Sequence
和this
被綁定到調用它的對象。有什麼建議麼?
編輯
沒有得到相同的結果,所以我現在可以俯瞰我的代碼, 所以我已經編輯我的實際的JavaScript庫中。它現在正在做。
打開開發人員面板以閱讀控制檯。按ctrl+a
然後b,然後按ctrl+b
按a,不顯示任何,所以按b和它的運行ctrl+a
seq功能。
發表一些小提琴,如果可以的話!我無法確切地得到你的問題... – Buzinas
是的,我總是有問題給我的問題。我會得到一個小提琴去 – EasyBB
添加我的實際JavaScript代碼的斌。 – EasyBB