Javascript 中面向方面的东西

发布于 2024-12-20 21:54:14 字数 1103 浏览 2 评论 0原文

因此,我编写了这段代码来帮助我在其他函数之后或之前添加函数,但我找不到更好的方法来做到这一点,我不得不使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘦怹 2024-12-27 21:54:14

我有一个名为 fcombine 的实用程序库,它支持

f1 = fcombine.pre(new_function, f1);
f1 = fcombine.post(f1, new_function);

您的代码丑陋的原因是因为您没有返回新函数。

您的代码的问题还在于它使用 eval 并尝试做太多的魔术。传递一个变量名字符串然后对其进行评估的整个想法绝对是可怕的。

您可以很容易地编写

hello = Aspects.append(hello, new_function);

fcombine.post 所示

I have a utility library called fcombine which supports

f1 = fcombine.pre(new_function, f1);
f1 = fcombine.post(f1, new_function);

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

琉璃繁缕 2024-12-27 21:54:14

怎么样,

function fnAppend(old_fn_name, new_fn){
    var old_fn = window[old_fn_name];
    window[old_fn_name] = function(){
        old_fn();
        new_fn();
    }
}

hello = function(){
    console.log("hello world!");
}

hello(); // hello world!
fnAppend('hello', function(){
    console.log("bye world!");
});
hello(); // hello world! bye world

只是为了展示该方法,函数父窗口也可以是 fnAppend 函数的可选参数。只需更改顺序即可为您提供 fnPrepend


编辑

function fnAppend(old_fn_name, new_fn, obj){
    obj = obj || window;
    var old_fn = obj[old_fn_name];
    obj[old_fn_name] = function(){
        old_fn.apply({},arguments);
        new_fn.apply({},arguments);
    }
}

what about this,

function fnAppend(old_fn_name, new_fn){
    var old_fn = window[old_fn_name];
    window[old_fn_name] = function(){
        old_fn();
        new_fn();
    }
}

hello = function(){
    console.log("hello world!");
}

hello(); // hello world!
fnAppend('hello', function(){
    console.log("bye world!");
});
hello(); // hello world! bye world

just to show the approach, the function parent window can also be an optional argument to the fnAppend function. Just changing the order will give you fnPrepend


EDIT

function fnAppend(old_fn_name, new_fn, obj){
    obj = obj || window;
    var old_fn = obj[old_fn_name];
    obj[old_fn_name] = function(){
        old_fn.apply({},arguments);
        new_fn.apply({},arguments);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文