• <i id='gt29D'><tr id='gt29D'><dt id='gt29D'><q id='gt29D'><span id='gt29D'><b id='gt29D'><form id='gt29D'><ins id='gt29D'></ins><ul id='gt29D'></ul><sub id='gt29D'></sub></form><legend id='gt29D'></legend><bdo id='gt29D'><pre id='gt29D'><center id='gt29D'></center></pre></bdo></b><th id='gt29D'></th></span></q></dt></tr></i><div id='gt29D'><tfoot id='gt29D'></tfoot><dl id='gt29D'><fieldset id='gt29D'></fieldset></dl></div>

      1. <tfoot id='gt29D'></tfoot>
        • <bdo id='gt29D'></bdo><ul id='gt29D'></ul>

      2. <small id='gt29D'></small><noframes id='gt29D'>

        <legend id='gt29D'><style id='gt29D'><dir id='gt29D'><q id='gt29D'></q></dir></style></legend>

        以编程方式将代码添加到 javascript 函数

        时间:2023-10-20

          <bdo id='mkyDF'></bdo><ul id='mkyDF'></ul>
          <tfoot id='mkyDF'></tfoot>

            <i id='mkyDF'><tr id='mkyDF'><dt id='mkyDF'><q id='mkyDF'><span id='mkyDF'><b id='mkyDF'><form id='mkyDF'><ins id='mkyDF'></ins><ul id='mkyDF'></ul><sub id='mkyDF'></sub></form><legend id='mkyDF'></legend><bdo id='mkyDF'><pre id='mkyDF'><center id='mkyDF'></center></pre></bdo></b><th id='mkyDF'></th></span></q></dt></tr></i><div id='mkyDF'><tfoot id='mkyDF'></tfoot><dl id='mkyDF'><fieldset id='mkyDF'></fieldset></dl></div>

              1. <legend id='mkyDF'><style id='mkyDF'><dir id='mkyDF'><q id='mkyDF'></q></dir></style></legend>
                  <tbody id='mkyDF'></tbody>
                • <small id='mkyDF'></small><noframes id='mkyDF'>

                  本文介绍了以编程方式将代码添加到 javascript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在不修改原始 JS 代码的情况下自定义现有的 JS 库.这段代码加载到我可以访问的几个外部 JS 文件中,我想做的是更改原始文件中包含的函数之一,而无需将整个内容复制并粘贴到第二个 JS 文件中.
                  所以举例来说,禁区JS可能有这样一个函数:

                  I'm attempting to customize an existing JS library without modifying the original JS code. This code loads in a few external JS files which I do have access to, and what I'd like to do is change one of the functions contained in the original file without copying and pasting the whole thing into the second JS file.
                  So for example, the off limits JS might have a function like this:

                  var someFunction = function(){
                      alert("done");
                  }
                  

                  我希望能够以某种方式将一些 JS 代码附加或预先添加到该函数中.原因主要是在原始不可触碰的 JS 中,函数非常庞大,如果该 JS 得到更新,我覆盖它的函数将过时.

                  I'd like to be able to somehow append or prepend some JS code into that function. The reason is primarily that in the original untouchable JS the function is pretty enormous and if that JS ever gets updated, the function I overwrite it with will be out of date.

                  我不完全确定这是可能的,但我想我会检查一下.

                  I'm not entirely sure this is possible, but I figured I'd check.

                  推荐答案

                  如果 someFunction 是全局可用的,那么你可以缓存函数,创建你自己的,然后让你调用它.

                  If someFunction is globally available, then you can cache the function, create your own, and have yours call it.

                  如果这是原版的话……

                  someFunction = function() {
                      alert("done");
                  }
                  

                  你会这样做......

                  You'd do this...

                  someFunction = (function() {
                      var cached_function = someFunction;
                  
                      return function() {
                          // your code
                  
                          var result = cached_function.apply(this, arguments); // use .apply() to call it
                  
                          // more of your code
                  
                          return result;
                      };
                  })();
                  

                  这是小提琴

                  请注意,我使用 .apply 来调用缓存函数.这让我可以保留 this 的预期值,并将传入的任何参数作为单独的参数传递,而不管有多少.

                  Notice that I use .apply to call the cached function. This lets me retain the expected value of this, and pass whatever arguments were passed in as individual arguments irrespective of how many there were.

                  这篇关于以编程方式将代码添加到 javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何附加 &lt;script&gt;&lt;/script&gt;在 JavaScri 下一篇:jQuery Uniform Checkbox 不(取消)选中

                  相关文章

                  <tfoot id='F6rl7'></tfoot>
                    <bdo id='F6rl7'></bdo><ul id='F6rl7'></ul>
                  <legend id='F6rl7'><style id='F6rl7'><dir id='F6rl7'><q id='F6rl7'></q></dir></style></legend>
                  <i id='F6rl7'><tr id='F6rl7'><dt id='F6rl7'><q id='F6rl7'><span id='F6rl7'><b id='F6rl7'><form id='F6rl7'><ins id='F6rl7'></ins><ul id='F6rl7'></ul><sub id='F6rl7'></sub></form><legend id='F6rl7'></legend><bdo id='F6rl7'><pre id='F6rl7'><center id='F6rl7'></center></pre></bdo></b><th id='F6rl7'></th></span></q></dt></tr></i><div id='F6rl7'><tfoot id='F6rl7'></tfoot><dl id='F6rl7'><fieldset id='F6rl7'></fieldset></dl></div>
                    1. <small id='F6rl7'></small><noframes id='F6rl7'>