我有一个
javascript
文件以开头
require()
,在
vscode
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();
但是,使用script标记将此脚本包含在HTML文件中,并在Google Chrome等浏览器中打开它,会生成一个关于的控制台错误
require() is not defined