简体   繁体   English

在 JavaScript 或 jQuery 中将代码动态附加到函数的末尾

[英]Dynamically append code to end of a function in JavaScript or jQuery

I have following JavaScript with some html.我有一些 html 跟随 JavaScript。 A sample demo can be found at following URL: http://js.do/sun21170/92329可以在以下 URL 找到示例演示: http : //js.do/sun21170/92329

My ambitious goal is to execute some code just after the if else part in doSomething function.我雄心勃勃的目标是在doSomething函数中的if else部分之后执行一些代码。 I have no control on what is inside this function since it's in a library I use but I do want to execute some code after the last line of code is executed in this function.我无法控制此函数中的内容,因为它位于我使用的库中,但我确实想在此函数中执行最后一行代码后执行一些代码。 So, it's like I want to dynamically insert some JavaScript code at end of doSomething function.所以,这就像我想在doSomething函数的末尾动态插入一些 JavaScript 代码。

Question : Is it possible to dynamically insert some code after last line of code in doSomething function and if yes, then how would I do it?问题:是否可以在doSomething函数的最后一行代码之后动态插入一些代码,如果是,那么我该怎么做?

For example, I want to insert following line of custom code: alert(val);例如,我想插入以下自定义代码行: alert(val); or if accessing the variables is out-of-question then insert just some other custom code like alert("hello");或者如果访问变量是没有问题的,那么只需插入一些其他自定义代码,如alert("hello"); . . I tried getting the body of callback function and then I guess I could append my new code, but still cannot implement my very generic requirement.我尝试获取回调函数的主体,然后我想我可以附加我的新代码,但仍然无法实现我非常通用的要求。

<button type="button" onclick="callback(200);">Callback gets called </button>
<script>
var callback = null;
//the function below cannot be changed
function doSomething(val) {
    if (val > 150) {
        alert("You are not eligible for this reward");
    } else {
        alert("Congrats! You have just been rewarded");
    }
}


//set callback to something
callback = doSomething;

//I want to automatically execute some code when callback finishes execution without 
//having to explicitly write that code within the doSomething function OR
//writing code after doSomething function is called in onclick function of button
//If this is possible, HOW WOULD I IMPLEMENT THIS?


//I can get the body of a function using below code
function getCallBackBody(callbackFunction) {
    var entire = callbackFunction.toString();
    var body = entire.substring(entire.indexOf("{") + 1, entire.lastIndexOf("}"));
    return body;
}
</script>

You can replace doSomething with a function that calls doSomething and then calls your code.您可以将 doSomething 替换为调用 doSomething 然后调用您的代码的函数。 Make sure doSomething is defined first.确保首先定义了 doSomething。

 function doSomething(val) { if (val > 150) { alert("You are not eligible for this reward"); } else { alert("Congrats! You have just been rewarded"); } } var orig_doSomething = window['doSomething']; window['doSomething'] = function () { orig_doSomething.apply(null, arguments); // your code, or a call to your function alert("Wow, doSomething just finished executing with these arguments - " + JSON.stringify(arguments)); };
 <button type="button" onclick="doSomething(200);">Callback gets called </button>

It's possible to create new function with new Function(newBody) , but it is very-very horrible solution.可以使用new Function(newBody)创建新函数,但这是非常非常糟糕的解决方案。 Don't do that, if it's possible.如果可能,请不要这样做。

You can construct your function with new body using Function constructor, like so:您可以使用 Function 构造函数构造具有新主体的函数,如下所示:

function injectToCallback(callbackFunction, newBody) {
  var entire = callbackFunction.toString();
  var args = entire.match(/^function.+\((.+)\)/);
  var body = entire.substring(entire.indexOf("{") + 1, entire.lastIndexOf("}")) + newBody;
  return new Function(args ? args[1] : "", body);
}

However you should notice that it's some kind of eval, so make sure you don't pass data provided by third-party things, ie users.但是你应该注意到它是某种 eval,所以确保你没有传递第三方提供的数据,即用户。

I do not recommend using this.推荐使用这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM