![](/img/trans.png)
[英]How do I use variables and function from another file in javascript?
[英]How can I use a function from one Javascript file in another?
我有以下Javascript文件one.js:
function doSomething() {
console.log("doing stuff");
}
我还有另一个Javascript文件two.js,在其中我想调用doSomething()
:
doSomething();
我该如何实现? 请注意,此代码用于浏览器扩展,因此我无法使用html。
您必须从one.js
import
函数,如下所示:
import doSomething from "one.js";
然后将其分配给当前对象的某个变量并使用它。
this.doSomething = doSomething();
您可以在扩展代码中使用脚本作为背景脚本和内容脚本,可以执行类似的操作,
"background": {
"scripts": [ "js/sharedFunctions.js", "js/mainBackground.js" ]
},
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["js/sharedFunctions.js", "js/mainContentScript.js"]
}
]
然后可以按顺序在第二个文件中使用第一个文件的功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.