How to access functions defined in ES6 module in a browser's JavaScript console?

和自甴很熟 提交于 2019-12-22 04:02:33

问题


I have a function that is defined in an ES6 module (sender.js) as follows:

function send() {
   // do stuff
}
export {send};

This module is then used in the application's main JavaScript file app.js as follows:

import {send} from "./sender"

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

How can I import the send function in the JavaScript console?


回答1:


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?



来源:https://stackoverflow.com/questions/44355014/how-to-access-functions-defined-in-es6-module-in-a-browsers-javascript-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!