代码之家  ›  专栏  ›  技术社区  ›  SaidbakR

VS代码调试控制台和任何web浏览器有什么区别

  •  0
  • SaidbakR  · 技术社区  · 6 年前

    我有一个 文件以开头 require() ,在

    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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Sanjeev    6 年前

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

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