简体   繁体   English

在不使用HTML的情况下与其他Java脚本共享JavaScript文件资源

[英]Share JavaScript file resources with another Javascript without using HTML

I need to use the functions of a javaScript file in another javaScript file, but I do not have an HTML linking the 2, how to share it? 我需要在另一个javaScript文件中使用一个javaScript文件的功能,但是我没有HTML链接2,如何共享它?

const {Builder} = require('selenium-webdriver')
var chrome = new Builder().forBrowser('firefox').build()
login(chrome);

this is my code main and i need get this other method to other JavaScript File. 这是我的代码主体,我需要将此其他方法获取到其他JavaScript文件。

function login(chrome, user, senha){
chrome.get('https://testerbeta.crm2.dynamics.com/main.aspx');
}

now the code is small, but the software that I have to test is great and I need to separate the methods in different javascripts files 现在的代码很小,但是我要测试的软件很棒,我需要将方法分离到不同的javascripts文件中

I think what you're looking for are import and export , added in ES6. 我认为您要寻找的是importexport ,并添加到ES6中。

Basically, in the file you want to get your functions from, just add export keyword before function declarement, just like that: 基本上,在要从中获取函数的文件中,只需在函数声明之前添加export关键字,如下所示:

export function foo() {
    return bar;
}

Then, in the file you want to inject your function in, you add this at the beginning of the file: 然后,在要插入函数的文件中,将其添加到文件的开头:

import { foo } from 'path/to/your/file_with_function_to_export.js'

More about that you can read here . 有关更多信息,您可以在这里阅读。

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

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