什么是dojo 1.7 AMD框架的好处? [英] what's the benefit of the dojo 1.7 AMD framework?

查看:131
本文介绍了什么是dojo 1.7 AMD框架的好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读关于dojo 1.7加载程序如何使用AMD API /框架的这里这里也,我在其中一张幻灯片中遇到了这个报价:AMD ()的最大好处是无法按需加载脚本,正如有些人可能认为的那样,最大的好处是代码组织/模块化的增加,以及对全局变量/命名空间的需求的减少。但是我的问题是,如果需要访问另一个函数的执行上下文(以及另一个函数的私有变量),则不能使用普通的js函数来避免全局变量,还有dojo.hitch())?换个方法,除了异步加载你需要的东西,什么是AMD框架的好处?

I have been reading about how the dojo 1.7 loader uses an AMD API/framework here and here too, and I came across this quote on one of the slides: "AMD(’s) greatest benefit isn’t being able to load scripts on-demand, as some people may think, the greatest benefit is the increase of the code organization/modularity and also the reduced need for globals/namespacing." But my question is, can't global variables already be avoided by using normal js functions, and maybe dojo.hitch() if you need to access another function's execution context (and another function's 'private' variables)? Put another way, other than asynchronously loading only what you need, what is the benefit of the AMD framework?

推荐答案

AMD的好处是拥有模块系统的优点,类似于命名空间系统在其他语言。在JavaScript中,我们经常使用全局变量伪造这个模块,但模块提供了一些特定的优势:

The benefits of AMD are the benefits of having a module system, analogous to a namespace system in other languages. In JavaScript, we often faked this with global variables, but modules give a number of specific benefits:


这些模块提供了他们顶部的隐私范围,从其他模块导入单例对象的设施,并导出自己的API。

These modules are offered privacy of their top scope, facility for importing singleton objects from other modules, and exporting their own API.

---从 CommonJS Modules / 1.1.1 spec ,它们全部启动。

--- From the CommonJS Modules/1.1.1 spec, which started it all.

这里的关键是进出口设施。以前,每个人都在做这个ad-hoc,全局变量(如 window.jQuery 窗口._ 等) 。要获取jQuery的导出功能,您必须知道魔术名称,希望没有人与之冲突,并确保在脚本之前加载了jQuery脚本。没有办法声明性地指定你对jQuery的依赖,而jQuery没有办法说这是我导出的,除了将它们填充到全局 window.jQuery 对象。

Key here is the import and export facilities. Previously everyone was doing this ad-hoc, with globals (like window.jQuery, window._, etc.). To get at jQuery's exported functionality, you had to know the magic name, hope nobody conflicted with it, and be sure that the jQuery script was loaded before your script. There was no way of declaratively specifying your dependency on jQuery, and jQuery had no way of saying "this is what I export" apart from just stuffing them onto a global window.jQuery object.

模块格式修复了这一点:每个模块都会导出特定的功能,例如

A module format fixes this: each module exports specific functions, e.g.

// math.js
define(function (require, exports, module) {
    exports.add = function (a, b) { return a + b; };
});

,每个模块可能需要特定的其他模块,例如

and each module can require specific other modules, e.g.

// perimeter.js
define(function (require, exports, module) {
    var math = require("math");

    exports.square = function (side) {
        return math.add(math.add(side, side), math.add(side, side));
    };
});






为什么AMD应该是模块系统的选择,作者 RequireJS 作为一名AMD加载器的作者James Burke很像Dojo已经写过一篇博文,详细介绍了他为什么认为AMD是最好的。


On why AMD should be the module system of choice, James Burke, the author of RequireJS---an AMD loader much like Dojo has---wrote a blog post detailing why he thinks AMD is the best.

这篇关于什么是dojo 1.7 AMD框架的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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