用一个新的构造函数像这样绑定一个参数?
Binding an argument as this, with a new constructor?
我正在尝试将 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
将 this 属性 绑定为调用 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 函数。
我可以告诉你的一个主要问题是使用相同的 default
对象,通过它你的 commands.cmd[combinator]
将指向最后出现的同一个对象。
在分配之前复制default
var def = Object.create(defaults);
for(var option in options)
{
if(option !== "executed" && option !== "called")
{
def[option] = options[option];
}
}
在意识到我做错了什么之后,感谢 code-jaff,我会接受他的回答,因为他确实给了我答案。除了没有使用 Object.create
,我只是创建了一个新的对象文字。
var def = {};
for(var option in defaults)
{
if(options.hasOwnProperty(option) && option !== "executed" && option !== "called" && option !== "waiting")
{
def[option] = options[option];
} else {
def[option] = defaults[option];
}
}
我正在尝试将 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
将 this 属性 绑定为调用 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 函数。
我可以告诉你的一个主要问题是使用相同的 default
对象,通过它你的 commands.cmd[combinator]
将指向最后出现的同一个对象。
在分配之前复制default
var def = Object.create(defaults);
for(var option in options)
{
if(option !== "executed" && option !== "called")
{
def[option] = options[option];
}
}
在意识到我做错了什么之后,感谢 code-jaff,我会接受他的回答,因为他确实给了我答案。除了没有使用 Object.create
,我只是创建了一个新的对象文字。
var def = {};
for(var option in defaults)
{
if(options.hasOwnProperty(option) && option !== "executed" && option !== "called" && option !== "waiting")
{
def[option] = options[option];
} else {
def[option] = defaults[option];
}
}