如何在 vscode 中调试 Javascript 函数?

问题描述

Macbook,OSX 10.9 VsCode - 企业版 2019

// 问题描述

我有一个非常简单的 JS 函数


function countup(n) {
    if (n < 1) {
      return [];
    } else {
      const countArray = countup(n - 1);
      //debugger;
      countArray.push(n);
      return countArray;
    }
  }
  console.log(countup(5));

我需要什么帮助?

通过在 vscode 之间添加断点,使用调试器在 vscode 上运行它。当我尝试使用 launch.json 配置


{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information,visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0","configurations": [
        {
            "type": "chrome","request": "launch","name": "Launch Chrome","file": "${workspaceRoot}/Test.js"
        }
    ]
} 

这基本上是在 Chrome 中渲染 JS 文件,但不执行实际功能。我已经在 vs 代码中使用本地服务器 + chrome 调试器扩展进行了相同的尝试。

我如何让它发挥作用?

解决方法

要运行 JS 代码,您需要打开一个新的 HTML 文件并运行 HTML 中的代码。 如果它只是一个基本代码,您可以使用 Script 标签 <script> 并使用 onload 事件调用 body 标签中的函数。 完成后,您可以使用 chrome 调试器(按 F12)

这里有一个例子

<body onload="Myfunction()">
    <script>
        function Myfunction() {
        console.log('hello world');
        }
    </script>
</body>