Javascript:扩展函数 [英] Javascript: Extend a Function
问题描述
类似这样的内容:
$ b $我想要的主要原因是我想扩展我的初始化函数。 b
// main.js
window.onload = init();
函数init(){
doSomething();
// extend.js
函数扩展init(){
doSomethingHereToo();
$ b所以我想扩展一个函数,比如我在PHP中扩展一个类。 / p>
我也想从其他文件扩展它,例如我在 main.js $ c $中有原始的init函数c>和 extended.js中的扩展函数。
解决方案更广泛地了解你实际上想要做什么以及你在做什么的背景,我相信我们可以给你一个比你的问题的文字更好的答案。 / p>
但是,这是一个字面上的答案:
如果您将这些函数分配给某处的某个属性,原来的功能,并把你的替换物业,而不是:
// main.js中的原始代码
var theProperty = init;
函数init(){
doSomething();
}
//通过在extended.js中替换和包装来扩展它
theProperty =(function(old){
function extendsInit(){
old();
doSomething();
}
return extendsInit;
})(theProperty);
如果你的函数不在一个对象上,你可能想把它放在那里方便以上。例如:
//在main.js中
var MyLibrary =(function(){
var publicSymbols = {};
publicSymbols.init = init;
函数init(){
}
return publicSymbols;
}) ();
//在extended.js
(function(){
var oldInit = MyLibrary.init;
MyLibrary.init = extendedInit;
function extendedInit (){
oldInit.apply(MyLibrary); //在`init`使用`this`的情况下使用#apply
doSomething();
}
})();
但是这样更好的方法来做到这一点。例如,提供注册 init
函数的方法。
/ / in main.js
var MyLibrary =(function(){
var publicSymbols = {},
initfunctions = [];
publicSymbols.init = init;
函数init(){
var funcs = initFunctions;
initFunctions = undefined;
for(index = 0; index< funcs.length ; ++ index){
try {funcs [index]();} catch(e){}
$ b publicSymbols.addInitFunction = addInitFunction;
函数addInitFunction(f){
if(initFunctions){
// Init尚未运行,记住它
initFunctions.push(f);
}
else {
//`init`已经运行,几乎立即调用它
//但是*异步*(因此调用者从不会同时看到
//调用)
套超时(f,0);
}
}
return publicSymbols;
})();
(以上大部分内容可以写得更简洁一点,但我想用清晰的名字,比如 publicSymbols
而不是我通常的 pubs
或者匿名对象文字。想要匿名功能,但我不太在乎匿名功能。)
The main reason why I want it is that I want to extend my initialize function.
Something like this:
// main.js
window.onload = init();
function init(){
doSomething();
}
// extend.js
function extends init(){
doSomethingHereToo();
}
So I want to extend a function like I extend a class in PHP.
And I would like to extend it from other files too, so for example I have the original init function in main.js
and the extended function in extended.js
.
解决方案 With a wider view of what you're actually trying to do and the context in which you're doing it, I'm sure we could give you a better answer than the literal answer to your question.
But here's a literal answer:
If you're assigning these functions to some property somewhere, you can wrap the original function and put your replacement on the property instead:
// Original code in main.js
var theProperty = init;
function init(){
doSomething();
}
// Extending it by replacing and wrapping, in extended.js
theProperty = (function(old) {
function extendsInit() {
old();
doSomething();
}
return extendsInit;
})(theProperty);
If your functions aren't already on an object, you'd probably want to put them there to facilitate the above. For instance:
// In main.js
var MyLibrary = (function() {
var publicSymbols = {};
publicSymbols.init = init;
function init() {
}
return publicSymbols;
})();
// In extended.js
(function() {
var oldInit = MyLibrary.init;
MyLibrary.init = extendedInit;
function extendedInit() {
oldInit.apply(MyLibrary); // Use #apply in case `init` uses `this`
doSomething();
}
})();
But there are such better ways to do that. Like for instance, providing a means of registering init
functions.
// In main.js
var MyLibrary = (function() {
var publicSymbols = {},
initfunctions = [];
publicSymbols.init = init;
function init() {
var funcs = initFunctions;
initFunctions = undefined;
for (index = 0; index < funcs.length; ++index) {
try { funcs[index](); } catch (e) { }
}
}
publicSymbols.addInitFunction = addInitFunction;
function addInitFunction(f) {
if (initFunctions) {
// Init hasn't run yet, rememeber it
initFunctions.push(f);
}
else {
// `init` has already run, call it almost immediately
// but *asynchronously* (so the caller never sees the
// call synchronously)
setTimeout(f, 0);
}
}
return publicSymbols;
})();
(Much of the above could be written a bit more compactly, but I wanted to use clear names like publicSymbols
rather than my usual pubs
or anonymous object literal. You can write it a lot more compactly if you want to have anonymous functions, but I don't much care for anonymous functions.)
这篇关于Javascript:扩展函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!