如何在浏览器的JavaScript控制台中访问ES6模块中定义的功能? [英] How to access functions defined in ES6 module in a browser's JavaScript console?
问题描述
我具有在ES6模块(sender.js
)中定义的功能,如下所示:
I have a function that is defined in an ES6 module (sender.js
) as follows:
function send() {
// do stuff
}
export {send};
该模块随后在应用程序的主JavaScript文件app.js
中使用,如下所示:
This module is then used in the application's main JavaScript file app.js
as follows:
import {send} from "./sender"
app.js
文件中提供了send
函数,但是在Firefox的Javascript控制台中它没有:
The send
function is available in the app.js
file, however it is not in Firefox's Javascript console:
> send
ReferenceError: send is not defined
如何在JavaScript控制台中导入send
函数?
How can I import the send
function in the JavaScript console?
推荐答案
您可以通过将特定功能分配给全局对象来将其设置为全局功能–
在浏览器中是window
.
You can set the specific function as global by assigning it to the global object –
in browsers it's window
.
import {send} from "./sender";
window.send = send;
请注意,尽管它可能在调试中很有用,但您不应在生产代码中使用它–请参阅为什么全局变量被认为是错误的练习吗?
Note that while it might be useful in debugging, you shouldn't use that in production code – see Why are global variables considered bad practice?
这篇关于如何在浏览器的JavaScript控制台中访问ES6模块中定义的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!