我希望能够执行一个功能,当另一个执行。就像它们绑定在一起一样。我尝试了一些东西,例如使用bind
,但这不起作用,因为它仅用于更改上下文。绑定功能,所以当一个执行另一个执行
这里是我想要做的 http://jsfiddle.net/4hdX5/
我希望能够执行一个功能,当另一个执行。就像它们绑定在一起一样。我尝试了一些东西,例如使用bind
,但这不起作用,因为它仅用于更改上下文。绑定功能,所以当一个执行另一个执行
这里是我想要做的 http://jsfiddle.net/4hdX5/
一个的jsfiddle叫它:
function test() {
alert('testing');
}
function test2() {
alert('testing2');
test();
}
test2();
小提琴:Fiddle
你可以做的是做出某种的功能池。我没有在这里快速&脏的对象常量:
var functionPool = {
pool: {},
add: function (name, pFunction) {
functionPool.pool[name] = pFunction;
},
execute: function (name) {
functionPool.pool[name].call();
},
executeAllBound: function(name) {
functionPool.pool[name].call();
// TODO
// then iterate over all not named name
}
};
function f1() {
alert(42);
}
function f2() {
alert(1337);
}
functionPool.add("firstAlert", f1);
functionPool.add("secondAlert", f2);
functionPool.execute("firstAlert");
functionPool.execute("secondAlert");
functionPool.execute1llBound("secondAlert");
您将它们保存为KV-对,你可以很容易地执行一个或全部,或者一个开始,不是遍历所有其他。
查看http://underscorejs.org/ – Malk