測試了你給我的例子無關使用jQuery,是純JavaScript。另外,要小心你在你的例子中所做的是......不對。試想一下:
var ReportManager {
...
replace: function(report) {
var preLoad = this.opts.hooks.preLoad.pop();
//stuff happens
//now I want to add the preLoad hook back
this.opts.hooks.preLoad.push(function(report) { preLoad(report); });
}
}
如果執行此:
replace(null);
replace({foo:'bar'});
replace(null);
你this.opts.hooks.preLoad
陣列看起來像這樣:
Array(
0: function(report) { return function(report) { return function(report) { ... } } }
)
因爲你是推裹進自己的功能每次執行你的代碼。我不知道爲什麼你需要pop
和push
它又回來了,但這看起來很奇怪。
另外,Javascript是一個非常靈活的語言;這意味着你可以做很多奇怪的東西,如
"hello".concat(" world"); // -> 'hello world'
0.toString(); // -> '0'
(function(a) { return a; })("foo"); // -> 'foo'
(function() { return false; })() || (function() { return true; })(); // -> true (executes both functions)
(function(i) { return [i*2,i*3,i*4]; })(2)[1]; // -> 6
$('selector')[0]; // ...
// etc.
這是以任何方式與jQuery? – rid
嗯,我想這只是更多的JavaScript語法 – stinkycheeseman