为什么只CommonJS的说是适用于非浏览器应用程序吗? [英] Why is CommonJS only said to be suitable for non-browser apps?

查看:259
本文介绍了为什么只CommonJS的说是适用于非浏览器应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不使用它作为一个的Javascript组件一般模式,包括浏览器执行的JavaScript?

Why not use it as a general component pattern for Javascript, including browser-executed Javascript?

一目了然,它似乎是模块化我目前工作的项目,其中包括了大量的Javascript code-基地,拥有大量的组件,其中一些与海誓山盟互动的好方法。

At a glance, it seems to be a good way to modularize the project I'm currently working on, which consists of a large Javascript code-base, with lots of components, some of which interact with eachother.

推荐答案

CommonJS的绝对是适合的浏览器,与一些注意事项。该CommonJS的模块模式是相当不错的(在我的偏见的意见),也是一个很好的踏脚石提出了和谐的ECMAScript(JavaScript语言的计划下一个版本)模块系统。具体地说,和声模块将不能访问到全局(窗口)的对象。

CommonJS is definitely suitable for the browser, with some caveats. The CommonJS module pattern is quite nice (in my biased opinion), and is also a good stepping stone to the module system proposed for ECMAScript Harmony (the planned next release of the JavaScript language). Specifically, Harmony modules won't have access to the global ("window") object.

这一些人声称CommonJS的模块不适合浏览器的原因是,它们不能经由与所述被加载; script>标记没有一些服务器端的帮助。例如,假设你有一个出口convertToHTML功能的降价库。然后,你可以做一个模块,看起来像这样:

The reason that some people claim CommonJS modules are not suitable for the browser is that they can't be loaded via a <script> tag without some server-side assistance. For example, imagine you have a markdown library that exports a "convertToHTML" function. You could then make a module that looks like this:

var convertToHTML = require("markdown").convertToHTML;
exports.mangleSomeText = function() {
    // do something then call convertToHTML
}

此不通过脚本标签工作的几个原因(范围并不包裹,所以convertToHTML将得到附着到窗口中,要求通常不会被定义,并且需要为每个模块单独创建出口)

This doesn't work via a script tag for a few reasons (the scope isn't wrapped, so convertToHTML would get attached to window, require wouldn't typically be defined and exports needs to be created separately for each module).

一个客户端库与服务器端的帮助一点点可能允许通过脚本标签加载轻松。或者,它通过XMLHtt prequest加载脚本,并执行一个eval()也将工作,但调试经验往往不是好客户端库。

A client side library with a tiny bit of server side help could allow this to load via script tags easily. Or, a client side library that loads the script via XMLHttpRequest and does an eval() would also work, though the debugging experience is often not as good.

一个比较合理的解决方案,现在,虽然一个是也CommonJS的成员之间有争议的辩论的主题,是 RequireJS 。使用RequireJS,你可以写你的模块是这样的:

A fairly reasonable solution right now, though one that is also the subject of contentious debate among CommonJS members, is RequireJS. Using RequireJS, you can write your module like this:

define(function(require, exports, module) {

var convertToHTML = require("markdown").convertToHTML;
exports.mangleSomeText = function() {
    // do something then call convertToHTML
}

});

我们所做是附加的模块周围定义()位。 (你可以可能会让一台服务器做pretty容易为好,这样你甚至都不需要手动键入定义的一部分)。

All we did was add that define() bit around the module. (You could likely make a server do that pretty easily as well, so that you don't even need to manually type the define part).

我个人现在用的一对夫妇的项目RequireJS,并发现这是一个简单的方法来利用CommonJS的模块,而服务器端位。还有的许多的其他解决方案,如果你不在运行的静态JS文件依赖,标准CommonJS的模块是一个伟大的路要走。

I've personally used RequireJS on a couple of projects now and find it an easy way to make use of CommonJS modules without a server-side bit. There are many other solutions and if you aren't reliant on running static JS files, standard CommonJS modules are a great way to go.

(ObDisclaimer:我开始了CommonJS的项目,所以我显然有失偏颇。)

(ObDisclaimer: I started the CommonJS project, so I am clearly biased.)

这篇关于为什么只CommonJS的说是适用于非浏览器应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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