Javascript 如何在运行时修改javascript代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7343276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:53:52  来源:igfitidea点击:

How to modify javascript code at run time?

javascriptdebuggingruntimemethodologyliveedit

提问by theninjagreg

Is there a way to modify JavaScript code while debugging? Visual Studio has "Edit and Continue", and similar hot swapping of code can be done in Java and other languages. Can this be done with JavaScript, and if so, how?

有没有办法在调试时修改 JavaScript 代码?Visual Studio 具有“编辑并继续”功能,类似的代码热交换可以在 Java 和其他语言中完成。这可以用 JavaScript 来完成吗,如果可以,怎么做?

采纳答案by icktoofay

Chrome, Safari, and some other WebKit-based browsers contain a feature in the Web Inspector known as Live Edit. If you go to the Scripts panel and are stopped on a breakpoint (or maybe even if not stopped on a breakpoint — I'm not sure), you can double click on a line and start editing that line. The changes you make will take effect on the script.

Chrome、Safari 和其他一些基于 WebKit 的浏览器在 Web Inspector 中包含一项称为实时编辑的功能。如果您转到“脚本”面板并在断点处停止(或者即使未在断点处停止 - 我不确定),您可以双击一行并开始编辑该行。您所做的更改将对脚本生效。

回答by wesbos

With Chrome Developer tools, this is super easy.

使用 Chrome 开发者工具,这非常容易。

Just pop open inspector, click on the scriptstab, select which one you want from the dropdown menu and then you are free to edit the script and add in breakpoints. If you refresh the page, your breakpoints will stay there.

只需弹出打开检查器,单击scripts选项卡,从下拉菜单中选择您想要的选项,然后您就可以自由编辑脚本并添加断点。如果您刷新页面,您的断点将保留在那里。

If you watch this talk but Paul Irish, he shows how you can edit a script on the fly

如果你不看这个演讲,除了 Paul Irish,他展示了如何即时编辑脚本

http://paulirish.com/2011/a-re-introduction-to-the-chrome-developer-tools/

http://paulirish.com/2011/a-re-introduction-to-the-chrome-developer-tools/

also good:

也很好:

http://blip.tv/jsconf/jsconf2011-paul-irish-5382827

http://blip.tv/jsconf/jsconf2011-paul-irish-5382827

回答by Chetan

If you're talking about while debugging, it's very easy to modify the running code. In your debugging console, you can enter in Javascript expressions and it will run in the context of the window, which contains all the objects and functions of your code, so you can swap them out by redefining them.

如果您在调试时谈论,那么修改正在运行的代码非常容易。在您的调试控制台中,您可以输入 Javascript 表达式,它将在窗口的上下文中运行,其中包含您代码的所有对象和函数,因此您可以通过重新定义它们来交换它们。

回答by MtnManChris

Because JavaScript can modify the DOM the you essentially have to change the JavaScript file, save it and reload. For me, I like IE so I run the webpage in a browser NOT IN DEBUG mode. Then you can change the script files, SAVE them. switch back to the browser and reload (F5) to see your changes. Supposedly IE 11 has this ability (probably like Chrome which is essentially what I am doing, changing saving reloading from what I can tell) but I can't get it to find, let alone open a js file. Super poor UI. I'm guessing the browser to open the file icon is enter image description herebut it is always grayed out.

因为 JavaScript 可以修改 DOM,所以您本质上必须更改 JavaScript 文件,保存并重新加载。对我来说,我喜欢 IE,所以我在浏览器中运行网页,而不是在调试模式下。然后您可以更改脚本文件,保存它们。切换回浏览器并重新加载 (F5) 以查看您的更改。据说 IE 11 具有这种能力(可能就像 Chrome 一样,这基本上就是我正在做的,从我能说的改变保存重新加载)但我无法找到它,更不用说打开一个 js 文件了。超级差的用户界面。我猜浏览器打开文件图标是,在此处输入图片说明但它总是变灰。