{"action":"opened","issue":{"url":"https://api.github.com/repos/fengcaian/NOTES/issues/33","repository_url":"https://api.github.com/repos/fengcaian/NOTES","labels_url":"https://api.github.com/repos/fengcaian/NOTES/issues/33/labels{/name}","comments_url":"https://api.github.com/repos/fengcaian/NOTES/issues/33/comments","events_url":"https://api.github.com/repos/fengcaian/NOTES/issues/33/events","html_url":"https://github.com/fengcaian/NOTES/issues/33","id":300125816,"number":33,"title":"AMD CMD CommonJS","user":{"login":"fengcaian","id":20163896,"avatar_url":"https://avatars3.githubusercontent.com/u/20163896?v=4","gravatar_id":"","url":"https://api.github.com/users/fengcaian","html_url":"https://github.com/fengcaian","followers_url":"https://api.github.com/users/fengcaian/followers","following_url":"https://api.github.com/users/fengcaian/following{/other_user}","gists_url":"https://api.github.com/users/fengcaian/gists{/gist_id}","starred_url":"https://api.github.com/users/fengcaian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fengcaian/subscriptions","organizations_url":"https://api.github.com/users/fengcaian/orgs","repos_url":"https://api.github.com/users/fengcaian/repos","events_url":"https://api.github.com/users/fengcaian/events{/privacy}","received_events_url":"https://api.github.com/users/fengcaian/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-26T06:43:46Z","updated_at":"2018-02-26T06:43:46Z","closed_at":null,"author_association":"OWNER","body":"AMD CMD CommonJS是目前最常用的三种模块化书写规范。前端模块化就是把复杂的文件分成一个个独立的模块，比如JS文件，分成独立的模块后有利于代码的重用和维护，\r\n但是这样又会引来模块与模块之间的相互依赖的问题，所有就有了这三种规范\r\nCommonJS\r\nCommonJS规范是诞生比较早的。NodeJS就采用了CommonJS，这样使用的：\r\n```javascript\r\nvar clock = require('clock');\r\nclock.start();\r\n```\r\n这种写法适合服务器端，因为在服务器端读取模块都是在本地磁盘，加载速度很快。但是如果在客户端，加载模块的时候就可能受到网络的影响变得很慢，比如上面的例子中clock\r\n必须等到clock.js请求成功，加载完毕。\r\nAMD\r\nAMD，即（Asynchronous Module Definition）,这种规范是异步加载模块，requireJS应用了这种规范。先定义所有依赖，然后在加载完成后的回调函数中执行：\r\ndefine([module], callback);\r\n用AMD写上一个模块：\r\n```javascript\r\ndefine(['clock'],function(clock){\r\n  clock.start();\r\n});\r\n```\r\nAMD虽然实现了异步加载，但是开始就把所有依赖写出来是不符合书写的逻辑顺序的，能不能像commonJS那样用的时候再require，而且还支持异步加载后再执行呢？\r\nCMD\r\nCMD，即（Common Module Definition）,SeaJS实现了这种规范，例子：\r\n```javascript\r\ndefine(function(require, exports, module) {\r\n   var clock = require('clock');\r\n   clock.start();\r\n});\r\n```\r\nAMD依赖前置（提前执行依赖），js可以方便知道依赖模块是谁；CMD就近依赖（按需加载），需要使用把模块变为字符串解析一遍才知道依赖了那些模块\r\n目前项目中使用的是requireJS,也就是使用的是AMD规范"}}
{"actor":{"display_login":"fengcaian"}}