为什么 Node.js 没有原生 DOM? [英] Why doesn't Node.js have a native DOM?

查看:25
本文介绍了为什么 Node.js 没有原生 DOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发现 Node.js 是使用 V8 JavaScript 引擎构建的时,我想:

<块引用>

太好了,网页抓取会更容易将像在浏览器中一样呈现,带有支持 XPath 和任何 AJAX 调用的原生"DOM执行的页面.

  1. 当它使用与 Chrome 相同的 JavaScript 引擎时,为什么它没有原生 DOM?
  2. 为什么它没有在检索到的页面中运行 JavaScript 的模式?
  3. 我对 JavaScript 引擎与 Web 浏览器中的引擎有何不了解?

非常感谢!

解决方案

DOM 就是 DOM,而 JavaScript 实现只是一个单独的实体.DOM 表示 Web 浏览器向 JavaScript 环境公开的一组设施.没有要求,但是任何特定的 JavaScript 运行时都将通过全局对象公开任何设施.

Node.js 是一个独立的 JavaScript 环境完全独立于网络浏览器.Web 浏览器和 JavaScript 之间没有内在联系;DOM 不是 JavaScript 语言或规范或任何东西的一部分.

我在基于 Java 的 Web 服务器中使用旧的基于 Rhino Java 的 JavaScript 实现.该环境也与任何 DOM 毫无关系.这是我自己的应用程序,负责用设施填充全局对象以执行我需要它能够执行的操作,而不是 DOM.

请注意,如果您想在 Node 项目中使用虚拟 DOM,则可以使用 jsdom 之类的项目.由于其作为服务器端平台的本质,DOM 是一种 Node 可以不用的工具,并且对于各种服务器应用程序仍然非常有意义.这并不是说 DOM 可能对某些人没有用处,只是它与流程控制、I/O、网络、数据库互操作等服务不在同一类别中.

对于为什么?"这个问题,可能会有一些官方"的答案.在那里,但这基本上只是维护 Node(现在是 Node 基金会)的人的事.如果一些勇敢的开发人员决定 Node 应该默认提供一组模块来支持虚拟 DOM,并且成功运行并实现了这一点,那么 Node 拥有一个 DOM.>

When I discovered that Node.js was built using the V8 JavaScript engine, I thought:

Great, web scraping will be easier as the page will be rendered like in the browser, with a "native" DOM supporting XPath and any AJAX calls on the page executed.

  1. Why doesn't it have a native DOM when it uses the same JavaScript engine as Chrome?
  2. Why doesn't it have a mode to run JavaScript in retrieved pages?
  3. What am I not understanding about JavaScript engines vs the engine in a web browser?

Many thanks!

解决方案

The DOM is the DOM, and the JavaScript implementation is simply a separate entity. The DOM represents a set of facilities that a web browser exposes to the JavaScript environment. There's no requirement however that any particular JavaScript runtime will have any facilities exposed via the global object.

What Node.js is is a stand-alone JavaScript environment completely independent of a web browser. There's no intrinsic link between web browsers and JavaScript; the DOM is not part of the JavaScript language or specification or anything.

I use the old Rhino Java-based JavaScript implementation in my Java-based web server. That environment also has nothing at all to do with any DOM. It's my own application that's responsible for populating the global object with facilities to do what I need it to be able to do, and it's not a DOM.

Note that there are projects like jsdom if you want a virtual DOM in your Node project. Because of its very nature as a server-side platform, a DOM is a facility that Node can do without and still make perfect sense for a wide variety of server applications. That's not to say that a DOM might not be useful to some people, but it's just not in the same category of services as things like process control, I/O, networking, database interop, and so on.

There may be some "official" answer to the question "why?" out there, but it's basically just the business of those who maintain Node (the Node Foundation now). If some intrepid developer out there decides that Node should ship by default with a set of modules to support a virtual DOM, and successfully works and works and makes that happen, then Node will have a DOM.

这篇关于为什么 Node.js 没有原生 DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆