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

标签 javascript browser visual-studio-code

我有一个文件以 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/

相关文章:

php - 为什么 Post/Redirect/Get 模式(PRG 模式)有效?

javascript - 浏览器如何在单页应用程序上缓存index.html

ruby-on-rails - VSCode 将调试 shell 更改为 Windows 上的 bash

javascript - Google 表单的动态 iframe 高度

javascript - 需要有关 JavaScript 正则表达式的帮助 :Regular Expression Too Complex

authentication - Telegram 登录小部件因禁用跨站点 cookie 而损坏

使用相对路径在 VSCode 中找不到 CSS/SCSS 代码模块

javascript - 检测js/css文件加载失败

javascript - 将 js 从 svg 文件中分离出来时出现问题

json - 我应该在 VS Code 中为 'json.maxItemsComputed' 设置什么?