- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关于 AMD(异步模块定义),我读到这样的阶段:
The AMD format comes from wanting a module format that was better than today's "write a bunch of script tags with implicit dependencies that you have to manually order" and something that was easy to use directly in the browser.
最佳答案
早在 JavaScript 获得原生模块系统之前,将脚本放到页面上的唯一方法是 <script>
元素。这些按顺序执行,按照它们在 HTML 中出现的顺序。这意味着如果你的脚本依赖于 jQuery,那么 jQuery 的 <script>
必须在脚本的 <script>
之前出现.否则,它会爆炸。
将应用程序逻辑拆分为多个文件的情况并不少见,尤其是随着应用程序的增长。但是使用这种手动排序脚本的系统很快就会变成一场噩梦。您的脚本具有隐式依赖关系,其管理在其他地方定义。这就是 AMD 的用武之地。
AMD 是一个模块规范,而 RequireJS 是这种系统的实现。简而言之,它是您代码的包装器,它 1) 在调用之前保持脚本惰性,2) 允许您的脚本显式定义其依赖项,以及 3) 允许模块系统计算出哪些依赖项以什么顺序执行。
这是一个粗略的例子:
// your-app.js
define(['jquery', 'underscore'], function($, _){
// Your script sits in this "wrapper" function.
// RequireJS now knows app.js needs jquery and underscore.
// It loads and executes them first before your script.
})
// jquery.js
define('jquery', [], function(){
// jQuery stuff
return jQuery
})
// underscore.js
define('underscore', [], function(){
// underscore stuff
return underscore
})
// Then on your HTML, load up your app.
<script data-main="path/to/app.js" src="path/to/require.js"></script>
关于javascript - AMD : what is the purpose in javascript context?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10160343/
AMD 似乎是按需加载 javascript 模块的最佳实践。 这应该适用于用户仅使用一部分可用功能的大型网络应用程序。 我读过有关优化器的信息,它将所有必需的模块连接到一个文件中,并且我读过有关无优
我在一个项目中使用 requirejs,并且有 2 个模块: a.js:是一个非 AMD 模块,我无法触及其代码 b.js:是我用 define() 函数编写的 AMD 模块。它需要 a.js 才能工
有没有办法导入或注释 Typescript 模块,以便在生成 AMD 兼容模块时自动将外部 AMD 模块作为依赖项包括在内?: tsc --module AMD example.ts 我尝试同时包含一
在我的项目中,有一个自定义第三方库(单个构建文件),他们使用几个非 AMD 和 AMD 文件创建了该库。并且大多数属性直接暴露给窗口对象。在我们的项目中,根据新的要求,我们必须使用 requireJS
我正在尝试创建一个由许多小文件(编译为 JavaScript-AMD 模块的 TypeScript 文件)构建的单个 AMD 库(模块)文件。 问题是: 我在类之间有循环依赖。 ( parent 需要
我有一个库 - 称之为 SomeLib - 它被定义为支持各种模块加载器: (function(global, factory) { if (typeof define === 'functi
一个星期以来,我开始学习如何将 require.js 与 Backbone.js 和 Underscore.js 一起使用。 这是一个非常困难的东西,但 3 天前我读到 Underscore 将不再支
main-bower-files无法抓取backbone-amd和underscore-amd ,有什么想法为什么以及如何解决它吗? gulpfile.js var mainBowerFiles =
我正在尝试使用 perf在我的带有 AMD cpu 的设备上,但我真的找不到任何关于如何获取的信息,比如说,cache-misses来自 AMD。我读到你需要写 -e rNNN ,其中 NNN是事件的
我对 AMD 波前和 OpenCL 的工作组大小的术语有些困惑。 我发现不同的来源做了不同的陈述。 我的问题是:AMDs GCN 技术的波前大小是多少,根据这个:opencl 上的工作组至少应该有多大
我正在尝试使用 Intern 测试 Require.js 项目。我在测试中遇到错误,其中在加载 jQuery 插件时未定义 jQuery。我的加载器配置如下所示: loader: { // A
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想要改善这个问题吗?更新问题,以便将其作为on-topi
Modernizr 是使用 AMD 定义编写的,这很好,因为这就是我当前项目所使用的,并且可以在需要时引入 Modernizr 依赖项。问题是,Modernizr 模块都需要一个平面文件夹层次结构,但
AMD 不久前宣布推出 Fusion 平台。读了一些相关内容后,我既兴奋又怀疑。例如,它应该使 GPU 和 CPU 共享相同的内存成为可能。 (GPU 和 CPU 都在同一个封装中)现在,由于 GPU
当我从 Android Studio 启动 AVD 时,它长时间显示黑色空白屏幕并且我看不到主屏幕。我查看了 2013 年之前的几个 Stack Overflow 帖子(Virtual Machine
AMD 是否允许您定义一个模块,其导出是动态的,具体取决于其他模块? 我遇到的问题是,即使正文中有 require 调用,define 也会立即返回。这意味着,如果模块的定义依赖于其他模块,则任何依赖
假设我有一个加载模块myModule的应用程序。所述模块需要一些其他资源才能工作(例如模板),因此 myModule 关心加载它所需的资源,这很巧妙。实际上,这很简单: // main.js requ
AMD 规范中是否有任何内容规定必需的模块必须提供相同的对象?假设所需的模块是提供给所有需要的模块的单个实例,这似乎是相当常见的做法,但是有什么可以防止模块加载器将加载的模块仅视为缓存(可能在某个时候
所以我现在一直在使用 require.js,但我意识到我实际上并不知道它在幕后是如何工作的。它说它是一个 AMD 加载器。 我知道 CommonJS 是同步的,这意味着它会在加载时阻止其他代码的执行。
有没有办法从*.ts 中设置编译模块的名称?一些 tsconfig、json 设置或一些可以执行此操作的 Gulp 插件。 目前我得到 define([], function(){ .. }) 但我不
我是一名优秀的程序员,十分优秀!