<bdo id='bFBZz'></bdo><ul id='bFBZz'></ul>

    1. <small id='bFBZz'></small><noframes id='bFBZz'>

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

        是否可以在不修改原始代码的情况下向现有的 javascript 函数添加一些代码?

        时间:2023-10-20

          <bdo id='L7Rk6'></bdo><ul id='L7Rk6'></ul>

          <tfoot id='L7Rk6'></tfoot>

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

                  <small id='L7Rk6'></small><noframes id='L7Rk6'>

                  <legend id='L7Rk6'><style id='L7Rk6'><dir id='L7Rk6'><q id='L7Rk6'></q></dir></style></legend>
                  本文介绍了是否可以在不修改原始代码的情况下向现有的 javascript 函数添加一些代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有一些javascript代码,例如

                  There is some javascript code, e.g.

                  function hello() {
                  }
                  function world() {
                  }
                  

                  我想向它们添加一些日志记录代码,但我不想修改代码.我希望我可以在另一个文件中编写一些代码,它会在运行时修改函数.可以这样做吗?

                  I want to add some logging code to them, but I don't want to modify the code. I hope I can write some code in another file, and it will modify the functions at runtime. It is possible to do this?

                  更新

                  感谢您的两个回答,但我必须让这个问题更清楚.

                  Thanks for the two answers, but I have to make this question clearer.

                  helloworld 函数只是一些示例,实际上文件中有数百个函数,它是手动重新定义它们的实现.

                  The hello and world functions are just some samples, actually there are hundreds of functions in the file, it's implement to redefine them manually.

                  我正在寻找一种自动执行此操作的方法(类似于 java 中的 AspectJ).

                  I'm looking for a way to do this automatically (similar to AspectJ in java).

                  推荐答案

                  你不能修改函数,但是你可以包装它们,用包装器替换函数.

                  You can't modify functions, but you can wrap them and replace the function with the wrapper.

                  这样的(参见现场演示):

                  function logFactory(func, message) {
                      return function () {
                          console.log(message);
                          return func.apply(this, arguments);
                      }
                  }
                  
                  hello = logFactory(hello, "Some log message");
                  

                  这不会让您获得任何数据虽然它正在被函数操作或更改函数内部发生的事情(尽管您可以捕获参数并在传递它们之前修改它们,并且可以捕获返回值并在返回之前对其进行修改).

                  This won't let you get any data while it is being manipulated by the function though or change what happens inside the function (although you can capture the arguments and modify them before passing them on, and you can capture the return value and modify it before returning it).

                  这篇关于是否可以在不修改原始代码的情况下向现有的 javascript 函数添加一些代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何打造“大"JavaScript(JQuery)中的div结构? 下一篇:jQuery 无法访问附加元素

                  相关文章

                  <small id='umpz5'></small><noframes id='umpz5'>

                  <legend id='umpz5'><style id='umpz5'><dir id='umpz5'><q id='umpz5'></q></dir></style></legend>
                    <tfoot id='umpz5'></tfoot>
                      <bdo id='umpz5'></bdo><ul id='umpz5'></ul>

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