Javascript 中面向方面的东西
因此,我编写了这段代码来帮助我在其他函数之后或之前添加函数,但我找不到更好的方法来做到这一点,我不得不使用 eval() 这确实不是一个好的做法。起初,我尝试做类似的事情:
Function.prototype.append = function(fn){
eval("this = function(){ ("+this.toString()+").apply(this, arguments); fn.apply(this, arguments); }");
}
hello = function(){
console.log("hello world!");
}
hello(); // hello world!
hello.append(function(){
console.log("bye world!");
});
hello(); // hello world! bye world
但它不起作用,因为该函数无法改变自身。所以我这样做了:
Aspects = new Object();
Aspects.append = function(aspect, fn){
eval(aspect + " = function(){ ("+eval(aspect + '.toString()')+").apply(this, arguments); fn.apply(this, arguments); }");
}
Aspects.prepend = function(aspect, fn){
eval(aspect + " = function(){ fn.apply(this, arguments); ("+eval(aspect + '.toString()')+").apply(this, arguments); }");
}
hello = function(){
console.log("hello world!");
}
hello(); // hello world!
Aspects.append('hello', function(){
console.log("bye world!");
});
hello(); // hello world! bye world!
我不想使用对象或任何东西,我只想在已经声明的函数之后或之前添加更多代码
So, i made this code to help me add functions after or before other functions, but i couldn't figure out a better way to do that, i had to use eval() and that is really not a good practice. At first, i tried to do something like:
Function.prototype.append = function(fn){
eval("this = function(){ ("+this.toString()+").apply(this, arguments); fn.apply(this, arguments); }");
}
hello = function(){
console.log("hello world!");
}
hello(); // hello world!
hello.append(function(){
console.log("bye world!");
});
hello(); // hello world! bye world
but it didn't work since the function can't change itself. So i did this:
Aspects = new Object();
Aspects.append = function(aspect, fn){
eval(aspect + " = function(){ ("+eval(aspect + '.toString()')+").apply(this, arguments); fn.apply(this, arguments); }");
}
Aspects.prepend = function(aspect, fn){
eval(aspect + " = function(){ fn.apply(this, arguments); ("+eval(aspect + '.toString()')+").apply(this, arguments); }");
}
hello = function(){
console.log("hello world!");
}
hello(); // hello world!
Aspects.append('hello', function(){
console.log("bye world!");
});
hello(); // hello world! bye world!
i don't want to work with objects or anything, i just want to add more code after or before my already declared function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个名为 fcombine 的实用程序库,它支持
您的代码丑陋的原因是因为您没有返回新函数。
您的代码的问题还在于它使用
eval
并尝试做太多的魔术。传递一个变量名字符串然后对其进行评估的整个想法绝对是可怕的。您可以很容易地编写
hello = Aspects.append(hello, new_function);
如
fcombine.post
所示I have a utility library called fcombine which supports
The reason your code is ugly is because your not returning the new function.
The problem with your code is also that it uses
eval
and tries to do too much magic. The whole idea of passing in a string of a variable name then evalling that is absolutely horrible.You can quite easily write
hello = Aspects.append(hello, new_function);
As demonstrated by
fcombine.post
怎么样,
只是为了展示该方法,函数父窗口也可以是 fnAppend 函数的可选参数。只需更改顺序即可为您提供
fnPrepend
编辑
what about this,
just to show the approach, the function parent
window
can also be an optional argument to thefnAppend
function. Just changing the order will give youfnPrepend
EDIT