Javascript 是否将脚本插入现有函数?

Javascript 是否将脚本插入现有函数?,javascript,jquery,Javascript,Jquery,此函数内置于页面中,我无法修改原始的.js文件: cool.lol = function () { // contents here } 有没有办法用我自己的脚本附加这个函数 像这样: cool.lol = function () { // contents here // i would like to add my own stuff here!!! } 或者,是否有一种方法可以让我检测到函数已被执行,这样我就可以在它之后运行一些东西?。更新为使用闭包并消

此函数内置于页面中,我无法修改原始的.js文件:

cool.lol = function () {

    // contents here

}
有没有办法用我自己的脚本附加这个函数

像这样:

cool.lol = function () {

    // contents here

    // i would like to add my own stuff here!!!
}
或者,是否有一种方法可以让我检测到函数已被执行,这样我就可以在它之后运行一些东西?

。更新为使用闭包并消除对临时变量的需要

//your cool with lol
var cool = {
    lol: function() {
        alert('lol');
    }
}

//let's have a closure that carries the original cool.lol
//and returns our new function with additional stuff

cool.lol = (function(temp) { //cool.lol is now the local temp
    return function(){       //return our new function carrying the old cool.lol
        temp.call(cool);     //execute the old cool.lol
        alert('bar');        //additional stuff
    }
}(cool.lol));                //pass in our original cool.lol

cool.lol();
cool.lol();​


您可以覆盖该功能:

// The file you can't touch has something like this
var cool = {};
cool.lol = function () {
    console.log("original");
}

// Keep a copy of the original function.
// Override the original and execute the copy inside
var original = cool.lol;
cool.lol = function () {
    original();
    console.log("modified");
}

// Call
cool.lol();​ // logs "original", then "modified".

在不创建污染公共范围的变量的情况下:

 //let's have your cool namespace
var cool = {
    lol: function() {
        alert('lol');
    }
}

//temporarily store
cool.lol_original = cool.lol;

//overwrite
cool.lol = function() {
    this.lol_original(); //call the original function
    alert('bar');    //do some additional stuff
}

cool.lol();​

JavaScript允许您

  • 以字符串形式获取函数的代码
  • 通过提供带有代码的字符串来创建新函数
每个对象都有一个
toString()
方法。对于函数,它返回它们的代码(除非重写)

返回
function(){//contents here}

让我们从这个字符串中提取函数体。它在
{
之后立即开始,包括除最后一个
}
之外的所有内容

var code = cool.lol.toString();
var body = code.substring(code.indexOf('{') + 1, code.length - 1);
然后我们添加更多的东西

var newBody = body + '// i would like to add my own stuff here!!!';
并使用
函数
构造函数创建一个新函数

当然,如果新函数还必须保留参数,那么还有更多的工作要做(您必须从函数代码中解析它们,然后将它们作为参数提供给
函数
构造函数)。为简单起见,在本例中,我假设函数没有参数

一个示例实现:


如果同样的答案确实是原因,那么为什么即使我的答案是第二名,第三名也没有被否决(叹气):(抱歉在那里大喊大叫……无论如何谢谢@Derek:)@NiftyDude有些人只是无缘无故地以否决票打了又跑。不管怎样,我向上投票是对向下投票的回应。匿名函数的使用很好。这里唯一需要注意的是,显然,您不能访问
cool.lol
的原始名称空间中的变量。@cheeken-hmm,不错的地方。我会试着用其他方法测试。有一个临时变量存在是不好的,尤其是。@JosephDreamer-为什么不使用
.call()
来引用
这个
?试试@Joseph这个梦想家的答案,肯定更好。最好避免使用函数构造函数。这是
eval
的孪生兄弟。@JosephDreamer是的,我理解传递给
函数的字符串必须是eval-ed。实际上,我不会使用这种方法。然而,我的解决方案是唯一一个在严格意义上将脚本插入到现有函数中的解决方案:-p我只是建议将此作为另一个函数包装的替代解决方案,正如其他受访者所建议的那样。这很公平。解决方案越多越好:)@Imp如果你不这么做,我至少要在你的回答中提到这一点,以及原因。虽然这是一个有趣的技巧,但如果在任何地方的生产代码中看到它,我都会很难过。
var code = cool.lol.toString();
var body = code.substring(code.indexOf('{') + 1, code.length - 1);
var newBody = body + '// i would like to add my own stuff here!!!';
cool.lol = new Function(newBody);