从控制台调用 Javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9054870/
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
Calling a Javascript Function from Console
提问by L3v3L
In Chrome's JavaScript console, how do I call a function that belongs to a .js file included in the webpage I am viewing?
在 Chrome 的 JavaScript 控制台中,如何调用属于我正在查看的网页中包含的 .js 文件的函数?
回答by Kevin Ennis
If it's inside a closure, i'm pretty sure you can't.
如果它在闭包内,我很确定你不能。
Otherwise you just do functionName();
and hit return.
否则,您只需执行functionName();
并按回车键即可。
回答by Tony Deacon
An example of where the console will return ReferenceError is putting a function inside a JQuery document ready function
控制台将返回 ReferenceError 的一个示例是将一个函数放入 JQuery 文档就绪函数中
//this will fail
$(document).ready(function () {
myFunction(alert('doing something!'));
//other stuff
}
To succeed move the function outside the document ready function
成功将函数移到文档就绪函数之外
//this will work
myFunction(alert('doing something!'));
$(document).ready(function () {
//other stuff
}
Then in the console window, type the function name with the '()' to execute the function
然后在控制台窗口中,输入带有'()'的函数名称以执行该函数
myFunction()
Also of use is being able to print out the function body to remind yourself what the function does. Do this by leaving off the '()' from the function name
同样有用的是能够打印出函数体来提醒自己函数的作用。通过从函数名称中去掉 '()' 来做到这一点
function myFunction(alert('doing something!'))
Of course if you need the function to be registered after the document is loaded then you couldn't do this. But you might be able to work around that.
当然,如果您需要在加载文档后注册该功能,那么您不能这样做。但你也许可以解决这个问题。
回答by Victoria Stuart
This is an older thread, but I just searched and found it. I am new to using Web Developer Tools: primarily Firefox Developer Tools (Firefox v.51), but also Chrome DevTools (Chrome v.56)].
这是一个较旧的线程,但我只是搜索并找到了它。我刚开始使用 Web 开发人员工具:主要是 Firefox 开发人员工具 (Firefox v.51),但也有 Chrome DevTools (Chrome v.56)]。
I wasn't able to run functions from the Developer Tools console, but I then found this
我无法从开发人员工具控制台运行功能,但我后来发现了这个
https://developer.mozilla.org/en-US/docs/Tools/Scratchpad
https://developer.mozilla.org/en-US/docs/Tools/Scratchpad
and I was able to add code to the Scratchpad, highlight and run a function, outputted to console per the attched screenshot.
我能够将代码添加到 Scratchpad,突出显示并运行一个函数,根据附加的屏幕截图输出到控制台。
I also added the Chrome "Scratch JS" extension: it looks like it provides the same functionality as the Scratchpad in Firefox Developer Tools (screenshot below).
我还添加了 Chrome 的“Scratch JS”扩展:看起来它提供了与 Firefox 开发者工具中的 Scratchpad 相同的功能(下面的截图)。
https://chrome.google.com/webstore/detail/scratch-js/alploljligeomonipppgaahpkenfnfkn
https://chrome.google.com/webstore/detail/scratch-js/alploljligeomonipppgaahpkenfnfkn
Image 1 (Firefox): http://imgur.com/a/ofkOp
图 1(火狐):http: //imgur.com/a/ofkOp
Image 2 (Chrome): http://imgur.com/a/dLnRX
图 2(Chrome):http: //imgur.com/a/dLnRX
回答by Adrian
I just discovered this issue. I was able to get around it by using indirection. In each module define a function, lets call it indirect
:
我刚刚发现了这个问题。我能够通过使用间接来解决它。在每个模块中定义一个函数,让我们调用它indirect
:
function indirect(js) { return eval(js); }
With that function in each module, you can then execute any code in the context of it.
有了每个模块中的这个函数,你就可以在它的上下文中执行任何代码。
E.g. if you had this import in your module:
例如,如果您的模块中有此导入:
import { imported_fn } from "./import.js";
You could then get the results of calling imported_fn
from the console by doing this:
然后,您可以imported_fn
通过执行以下操作从控制台获取调用结果:
indirect("imported_fn()");
Using eval
was my first thought, but it doesn't work. My hypothesis is that calling eval
from the console remains in the context of console, and we need to execute in the context of the module.
使用eval
是我的第一个想法,但它不起作用。我的假设是,eval
从控制台调用仍然在控制台的上下文中,我们需要在模块的上下文中执行。
回答by naresh
you can invoke it using window.function_name() or directly function_name()
您可以使用 window.function_name() 或直接 function_name() 调用它