简体   繁体   English

导出要在控制台运行的功能

[英]Exporting a function to be runned at Console

A few days ago i found a code with something similar to this. 几天前,我发现了与此类似的代码。 There is no big deal at it, but a little thing interested me. 没什么大不了的,但是有一点让我感兴趣。 It can be runned at the GoogleChrome Console. 可以在GoogleChrome控制台上运行。

As soon i serve the application with: npm run local 我通过以下方式为应用程序提供服务: npm run local

I can just go to the Console and type SoundModule.playMusic(); 我可以转到控制台并键入SoundModule.playMusic();。

"local": "node node_modules/rollup/bin/rollup src/client/main.js --o client/bundle.js --f es && node --max-old-space-size=8192 index" “本地”:“节点node_modules / rollup / bin / rollup src / client / main.js --o client / bundle.js --f es &&节点--max-old-space-size = 8192索引”

const SoundModule = {};

SoundModule.playMusic = async function(name) { .... };

export default SoundModule;

I'm really interested in how to write JS functions in my projects and run them using the Console, (since i don't know how to make it, i had to post a code snippet/node command) 我对如何在项目中编写JS函数并使用控制台运行它们真的很感兴趣(由于我不知道如何制作它,因此我不得不发布代码段/节点命令)

Here it is the project that bough my question into life https://github.com/johncintron/nodin 这是使我的问题变成现实的项目https://github.com/johncintron/nodin

Your best bet for making a function accessible in the console would be to attach it to the window object: 使功能在console可访问的最佳选择是将其附加到window对象:

window.myFunction = () => 'hello!'

window is the global scope for the browser, and its properties can be referenced directly in the console without having to include the namespace, so you could simply type myFunction() in the console and it should return "hello!" window是浏览器的全局作用域,它的属性可以直接在console引用,而不必包含名称空间,因此您只需在控制台中键入myFunction()即可,它应该返回"hello!" to you. 给你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM