我正在研究 Kyle Simpson 的“你不懂 JavaScript”系列。在(2014 年发布)“Scope & Closures”(第 62 页)的末尾,在 ES6 中有一个使用关键字“module”导入整个模块的示例,如下所示:
// import the entire "foo" and "bar" modules
module foo from "foo";
module bar from "bar";
console.log(
bar.hello( "rhino" )
);
foo.awesome();
Run Code Online (Sandbox Code Playgroud)
但是,此代码不起作用。我的问题是:module关键字是否经过试验和删除?我应该忘记这个关键字吗?
在 Visual Studio Code(不要与 Visual Studio 混淆)中有一个搜索界面(单击左上角的 mag glass)。
它有一个“要包含的文件”字段和一个“要排除的文件”字段。
我知道我可以通过输入文件名或使用像 *.js 这样的通配符来在搜索中包含或排除文件。
有没有办法使用这些字段专门包含或排除目录?如果没有,我该怎么做?
编辑:有人说这个问题可能类似于如何在搜索过程中选择要忽略的文件夹?
我认为这基本上是同一个问题,但正如我在最近对该问题的评论中所说:“这个问题的标题中没有任何内容表明您正在询问有关特定工具(Visual Studio Code)的问题。而且虽然您确实对其进行了标记visual-studio-code- 使其出现在有关该工具的搜索中 - 搜索者不一定知道这个问题与 VS Code 有关。此外,您使用的 Mac 特定键盘命令与手头的问题并不真正相关,并使问题看起来与非 Mac 用户无关。”
所以,是的,我想这是同一个问题。但我认为期望我找到它是不合理的。
我正在尝试使用 Karma 作为我的测试运行器、Mocha 作为我的测试框架、Sinon 作为我的模拟/存根/间谍库和 Chai 作为我的断言库来对一个函数进行单元测试。我在 Karma 配置中使用 Chromium 作为我的无头浏览器。
但是,对于为什么我收到以下错误,我完全感到困惑:
TypeError: Cannot redefine property: assign
Run Code Online (Sandbox Code Playgroud)
...当我对此运行 npm test 时:
function routeToNewPlace() {
const newLoc = "/accountcenter/content/ac.html";
window.location.assign(newLoc);
}
describe('tests', function() {
before('blah', function() {
beforeEach('test1', function() {
window.onbeforeunload = () => '';
});
});
it('routeToNewPlace should route to new place', function() {
expectedPathname = "/accountcenter/content/ac.html";
routeToNewPlace();
const stub = sinon.stub(window.location, 'assign');
assert.equal(true, stub.withArgs(expectedUrl).calledOnce);
stub.restore();
});
});
Run Code Online (Sandbox Code Playgroud)
如您所见,我试图为 window.location 分配一个空字符串,但这似乎没有帮助。
这是我的 karma.config.js:
module.exports = function(config) {
config.set({
frameworks: ['mocha', …Run Code Online (Sandbox Code Playgroud) 我正在使用第三方脚本,我需要为 window 对象分配一些函数,以便这些函数可用于该第三方脚本(将在同一个浏览器窗口中运行,从同一个域调用) )。我必须使用 ES6,使用let和 模块(import/ export)来做到这一点。
在 ES5 中,我可以这样做:
//index.html
<script src="main.js"></script>
//third-party script will use window.myObj.blurt()
<script src="third-party-script.js"></script>
Run Code Online (Sandbox Code Playgroud)
//main.js
var myObj = {};
myObj = {
blurt: function(){
console.log("Hello.");
}
}
Run Code Online (Sandbox Code Playgroud)
blurt()现在可以从window对象中调用。我可以把这个在我的浏览器控制台,它会工作:window.myObj.blurt()。
我想在 ES6 中这样做:
//index.html - note the type="module"
<script type="module" src="main.js"></script>
//third-party script will use window.myObj.blurt()
<script src="third-party-script.js"></script>
Run Code Online (Sandbox Code Playgroud)
//main.js
import './my-window-functions.js';
//other existing code
Run Code Online (Sandbox Code Playgroud)
//my-window-functions.js - NOT the third-party script - just my ES6 module system …Run Code Online (Sandbox Code Playgroud) javascript ×2
module ×2
chai ×1
ecmascript-6 ×1
karma-runner ×1
mocha.js ×1
search ×1
sinon ×1
unit-testing ×1