我有这种设置:我试图使用自定义事件处理程序处理.data
哈希和“垃圾收集”这是处理程序已经运行项目后,避免让他们不必要在内存中等.defer()
功能模拟我得到的,它生成的功能基于提供的异步运行,试图模仿本地事件处理的方式...
让我困惑的是,删除数据项分配... = null;
,要求延期功能之后,似乎之前的功能得到执行,基本上颠倒执行顺序(?!)运行,留下一个函数没有数据与...
工作3210 这里的脚本我有:异步功能“后清理”
//
//
var defer = function(func) {
// get asynchronoused `.func()` version
return function() {
// cache inputs for use by defered function
var args = arguments;
var node = this;
return setTimeout(
function() {
func.apply(node, args);
}
), node;
};
};
var action = function(node) {
// process a node
console.log(node['@foo']['txt']);
};
var data = {
'@foo': {
'id' : '#foo',
'tag' : 'h2',
'txt' : 'stuff',
},
'@spam': {
'id' : '#spam',
'tag' : 'h1',
'txt' : 'and shit',
},
};
// run `.action()` asynchronously
defer(action)(data);
// garbage collect after
data['@foo'] = null;
// this part seems to run before function call
// cleaning the data before `defer(action)(data);` gets it's time
// it throws error here because `null` gc-ed it right away (or something)
// TypeError: node['@foo'] is null
//
我目前正在围绕这一问题的工作由该会运行清理回调数据得到处理之后,这似乎很别扭提供额外的功能(
_finaly()
)和不自然到简单的事情..问题是为什么片段代码似乎运行不顺利,并且
这里究竟发生了什么或者是否有更简洁的方法来解决问题?
这里的解决方案到目前为止我有:
var action = function(node, _finaly) {
// provide `._finaly()` callback
// that will run after processing gets done
/////
console.log(node['@foo']['txt']);
// .. and run the cleanup code
_finaly && _finaly(node, 'txt');
};
defer(action)(data, function (node, item) {
// do the house-keep in a callback here
// rather than directly after execution
node.hasOwnProperty(item) && (node[item] = null);
});
//
// eof
由于它是*异步*! – Bergi
是的,你的解决方案与其他回调基本上是一条路。你甚至可能甚至不修改'action',而只是简单地包装它(因为它本身是同步的),并且可能使用其他一些更高级的函数来获得更好看的代码。 – Bergi