gpt4 book ai didi

javascript - VS Code 调试控制台与任何 Web 浏览器有什么区别

转载 作者:行者123 更新时间:2023-12-03 00:59:29 24 4
gpt4 key购买 nike

我有一个文件以 require() 开头,它在 中工作正常按 F5,它会在调试控制台中返回输出:

require('../src/FoxRandomString.js');
obj = FoxRandomString;
Tests = {
obj: obj,
canSetLength: function(){
vals = [true,false,-1,'text',undefined,null,0,2,4,10]
for (i = 0; i < vals.length; i++){
if (vals[i] < 4){
if (this.obj.setLength(vals[i]) == 4){
console.log('%cFor input value = '+vals[i]+ ' Success. Output: '+this.obj.setLength(vals[i]),'color: white; background:olive')
}
else{
console.log('%cFor input value = '+vals[i]+ ' Fail. Output: '+this.obj.setLength(vals[i]),'color:black; background:red')
}
}
else{
if (this.obj.setLength(vals[i]) == vals[i] || isNaN(vals[i])){
console.log('%cFor input value = '+vals[i]+ ' Success. Output: '+this.obj.setLength(vals[i]),'color: white; background:olive')
}
else{
console.log('%cFor input value = '+vals[i]+ ' Fail. Output: '+this.obj.setLength(vals[i]),'color:black; background:red')
}
}
}

}
}

Tests.canSetLength();

但是,使用脚本标记将此脚本包含在 HTML 文件中并在 Google Chrome 等浏览器中打开它,会生成有关 require() is not Defined 的控制台错误。我需要了解为什么会发生这种情况?

最佳答案

这是因为浏览器(客户端 JavaScript)中不存在 require()。您可以使用以下选项:

<script> tag.
CommonJS implementation, like Node.js
one more requireJS also can do the job done.

关于javascript - VS Code 调试控制台与任何 Web 浏览器有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52688335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com