Node环境和浏览器javascript环境之间的差异 [英] Differences between Node environment and browser javascript environment

查看:232
本文介绍了Node环境和浏览器javascript环境之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有点恼火,javascript项目有两个主要领域 - 节点和浏览器 - 虽然大多数浏览器JS都可以在Node内部轻松运行,如果需要,可以使用几个DOM来存储这些东西将Node内容移植到浏览器通常是事后的想法。

I've always been a bit annoyed that there are two major realms of javascript projects -- Node and "the browser" -- and while most browser JS can be easily run inside Node with a couple libraries for DOM stuff if needed, porting Node stuff to the browser is usually an afterthought.

这一切似乎都是开发人员社区浪费的精力,所有JS都可以减轻这种负担。开发人员只是为最小公分母(浏览器)开发,并使用各种填充程序来使用除了普通旧浏览器之外仅在Node或其他JS环境中可用的功能。

This all seems like a lot of wasted energy on the part of developer communities, which could be alleviated by all JS developers just developing for the "least common denominator" (the browser) and using various shims to use features only available in Node or other JS environments besides the plain old browser.

这不仅会削减很多生态系统,而且会使浏览器中的开发变得更加真实,这也使浏览器超级大国变得司空见惯......例如在 browserver ,它在浏览器中设置一个http服务器,但因为浏览器实际上无法接受ht tp请求,使用websockets与可以的代理节点服务器通信。

This would not only cut out a lot ecosystem cruft and make development-in-the-browser more realistic, it also makes it commonplace to give the browser superpowers…Look for example at browserver, which sets up an http server inside the browser, but because the browser cannot actually accept http requests, uses websockets to talk to a proxy Node server that can.

所以我想问一下,网络浏览器的javascript环境与Node的真正技术限制是什么?
我认为Node只是一个javascript环境,加上http服务器和本地文件系统,减去DOM和chrome。是否存在技术原因导致开发人员无法转向我上面描述的方法,为浏览器JS环境开发(这有官方名称吗?)并使用节点的填充程序?

So I want to ask, what are the real technical constraints of a web browser's javascript environment versus Node? I thought Node was just "a javascript environment, plus http server and local filesystem, minus the DOM and the chrome". Are there technical reasons why developers could not potentially move to the approach I described above, developing for the browser JS environment (does this have an official name?) and using shims for Node?

推荐答案

在客户端上运行的代码通常与服务器上运行的代码具有非常不同的目标。然而,当它有意义在两个环境中使用一些libarary的功能时,有很多使用通用AMD形式定义它们使它们与平台无关(例如 Q )。

Code that runs on the client usually have very different goals from the code that runs on the server. However when it makes sense to use some libarary's features in both environments there are a lot of them that are defined using a universal AMD form which makes them platform independent (e.g. Q).

两种环境之间的主要区别在于一个环境受到严格的保护政策和限制(浏览器),而另一个不是。浏览器也是安全相关操作的不可信任环境,例如强制执行安全权限。

The major difference between both environments is that one is subject to rigorous security policies and restrictions (browser) while the other isin't. The browser is also an untrustable environment for security-related operations such as enforcing security permissions.

我还会在这里添加@jfriend00评论,因为我认为它也非常相关暴露其他差异:

I'll also add @jfriend00 comment in here since I believe it's also very relevant a exposes other differences:


最大的实际区别是你必须设计一个浏览器
应用程序才能在已安装的基础上工作现有浏览器
包括旧版本(最小公分母)。部署
节点应用程序时,您可以选择您想要为其开发和部署的节点的ONE版本。这允许节点开发人员使用节点中最新的最大功能,这些功能将在一般浏览器群体中无法使用
多年。 @jfriend00

The biggest practical difference is that you have to design a browser application to work in an installed base of existing browsers including older versions (lowest common denominator). When deploying a node application, you get to select the ONE version of node that you want to develop for and deploy with. This allows node developers to use the latest greatest features in node which won't be available across the general browser population for years. @jfriend00

像浏览器这样的项目很有意思,我都在进行实验性开发,但它们在实践中真的有用吗?应该为他们真正有用的环境设计图书馆。在两种环境中提供所有库没有任何好处。不仅如此,它通常会导致代码复杂性增加,有些功能有时不会变质,导致平台之间的API不一致。

Projects like browserver are interesting and I am all for experimental development, but are they truly useful in practice? Libraries should be designed for the environment in which they are truly useful. There are no benefits in making all libraries available in both environments. Not only that it will generally result in an increased code complexity, some features will sometimes not be shimmable resulting in an inconsistent API between platforms.

这篇关于Node环境和浏览器javascript环境之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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