• NodeJS 环境和浏览器环境一样都是一个 JS 的运行环境, 都可以执行JS代码
  • 但是由于 宿主 的不同所以特点也有所不同
  • 浏览器环境中提供了 window 全局对象
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>浏览器中的window</title>
</head>
<body>
<script>
    console.log(window);
</script>
</body>
</html>
  • NodeJS 环境中的全局对象不叫 window, 而叫 global
console.log(global);

this的默认指向不同

  • 浏览器环境中的全局 this 默认指向 window
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>浏览器中的this</title>
</head>
<body>
<script>
    console.log(this);
</script>
</body>
</html>
  • NodeJS环境中的全局 this 默认指向空对象 {}
console.log(this);

API不同

  • 浏览器环境中提供了操作节点的 DOM 相关API和操作浏览器的 BOM 相关API
  • NodeJS环境中没有 HTML 节点也没有浏览器, 所以 NodeJS 环境中没有 DOM / BOM

相关文章:

  • 2022-12-23
  • 2022-01-25
  • 2021-10-29
  • 2022-12-23
  • 2021-05-07
  • 2022-12-23
  • 2021-11-06
  • 2021-09-29
猜你喜欢
  • 2021-11-09
  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-12-30
相关资源
相似解决方案