{"action":"created","issue":{"url":"https://api.github.com/repos/seajs/seajs/issues/259","repository_url":"https://api.github.com/repos/seajs/seajs","labels_url":"https://api.github.com/repos/seajs/seajs/issues/259/labels{/name}","comments_url":"https://api.github.com/repos/seajs/seajs/issues/259/comments","events_url":"https://api.github.com/repos/seajs/seajs/issues/259/events","html_url":"https://github.com/seajs/seajs/issues/259","id":5466706,"number":259,"title":"require 书写约定","user":{"login":"lifesinger","id":97227,"avatar_url":"https://avatars.githubusercontent.com/u/97227?v=3","gravatar_id":"","url":"https://api.github.com/users/lifesinger","html_url":"https://github.com/lifesinger","followers_url":"https://api.github.com/users/lifesinger/followers","following_url":"https://api.github.com/users/lifesinger/following{/other_user}","gists_url":"https://api.github.com/users/lifesinger/gists{/gist_id}","starred_url":"https://api.github.com/users/lifesinger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lifesinger/subscriptions","organizations_url":"https://api.github.com/users/lifesinger/orgs","repos_url":"https://api.github.com/users/lifesinger/repos","events_url":"https://api.github.com/users/lifesinger/events{/privacy}","received_events_url":"https://api.github.com/users/lifesinger/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/seajs/seajs/labels/documentation","name":"documentation","color":"444444"}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/seajs/seajs/milestones/11","html_url":"https://github.com/seajs/seajs/milestones/2.1.0","labels_url":"https://api.github.com/repos/seajs/seajs/milestones/11/labels","id":301627,"number":11,"title":"2.1.0","description":"","creator":{"login":"lifesinger","id":97227,"avatar_url":"https://avatars.githubusercontent.com/u/97227?v=3","gravatar_id":"","url":"https://api.github.com/users/lifesinger","html_url":"https://github.com/lifesinger","followers_url":"https://api.github.com/users/lifesinger/followers","following_url":"https://api.github.com/users/lifesinger/following{/other_user}","gists_url":"https://api.github.com/users/lifesinger/gists{/gist_id}","starred_url":"https://api.github.com/users/lifesinger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lifesinger/subscriptions","organizations_url":"https://api.github.com/users/lifesinger/orgs","repos_url":"https://api.github.com/users/lifesinger/repos","events_url":"https://api.github.com/users/lifesinger/events{/privacy}","received_events_url":"https://api.github.com/users/lifesinger/received_events","type":"User","site_admin":false},"open_issues":0,"closed_issues":42,"state":"closed","created_at":"2013-04-03T01:02:47Z","updated_at":"2013-08-13T02:01:34Z","due_on":"2013-06-30T07:00:00Z","closed_at":"2013-06-30T00:11:15Z"},"comments":57,"created_at":"2012-07-06T14:51:59Z","updated_at":"2016-09-01T02:20:53Z","closed_at":"2012-07-06T22:52:38Z","body":"# require 书写约定\r\n\r\n使用 Sea.js 书写模块代码时，需要遵循一些简单规则。\r\n\r\n> 只是书写和调试时的规范！！！构建后的代码完全不需要遵循下面的约定！！！！！！\r\n\r\n### 1. 正确拼写\r\n\r\n模块 factory 构造方法的第一个参数 **必须** 命名为 `require` 。\r\n\r\n```js\r\n// 错误！\r\ndefine(function(req) {\r\n  // ...\r\n});\r\n\r\n// 正确！\r\ndefine(function(require) {\r\n  // ...\r\n});\r\n```\r\n\r\n\r\n### 2. 不要修改\r\n\r\n不要重命名 `require` 函数，或在任何作用域中给 `require` 重新赋值。\r\n\r\n```js\r\n// 错误 - 重命名 \"require\"！\r\nvar req = require, mod = req(\"./mod\");\r\n\r\n// 错误 - 重定义 \"require\"!\r\nrequire = function() {};\r\n\r\n// 错误 - 重定义 \"require\" 为函数参数！\r\nfunction F(require) {}\r\n\r\n// 错误 - 在内嵌作用域内重定义了 \"require\"！\r\nfunction F() {\r\n  var require = function() {};\r\n}\r\n```\r\n\r\n\r\n### 3. 使用直接量\r\n\r\n`require` 的参数值 **必须** 是字符串直接量。\r\n\r\n```js\r\n// 错误！\r\nrequire(myModule);\r\n\r\n// 错误！\r\nrequire(\"my-\" + \"module\");\r\n\r\n// 错误！\r\nrequire(\"MY-MODULE\".toLowerCase());\r\n\r\n// 正确！\r\nrequire(\"my-module\");\r\n```\r\n\r\n在书写模块代码时，必须遵循这些规则。其实只要把 `require` **看做是语法关键字** 就好啦。\r\n\r\n\r\n## 关于动态依赖\r\n\r\n有时会希望可以使用 `require` 来进行条件加载：\r\n\r\n```js\r\nif (todayIsWeekend)\r\n  require(\"play\");\r\nelse\r\n  require(\"work\");\r\n```\r\n\r\n但请牢记，从静态分析的角度来看，这个模块同时依赖 play 和 work 两个模块，加载器会把这两个模块文件都下载下来。 这种情况下，推荐使用 `require.async` 来进行条件加载。\r\n\r\n\r\n## Why?\r\n\r\n这些约定初看起来会有些小不爽，其实也的确可以通过每次都编译的方式来去掉这些限制。但编译的方式，会给开发调试带来麻烦，代码的实现复杂度也会增加。Sea.js 的核心设计原则是保持简单，遵循 [New Jersey Approach](http://blog.jobbole.com/19062/)：\r\n\r\n>简单性：设计必须简单，这既是对实现的要求，也是对接口的要求。实现的简单要比接口的简单更加重要。简单是设计中需要第一重视的因素。\r\n\r\n因为简单，所以可靠！\r\n\r\n\r\n## 参考文档\r\n\r\n- [为什么要用构建工具来压缩 CMD 模块](https://github.com/seajs/seajs/issues/426)\r\n"},"comment":{"url":"https://api.github.com/repos/seajs/seajs/issues/comments/243957899","html_url":"https://github.com/seajs/seajs/issues/259#issuecomment-243957899","issue_url":"https://api.github.com/repos/seajs/seajs/issues/259","id":243957899,"user":{"login":"virtuecai","id":6389986,"avatar_url":"https://avatars.githubusercontent.com/u/6389986?v=3","gravatar_id":"","url":"https://api.github.com/users/virtuecai","html_url":"https://github.com/virtuecai","followers_url":"https://api.github.com/users/virtuecai/followers","following_url":"https://api.github.com/users/virtuecai/following{/other_user}","gists_url":"https://api.github.com/users/virtuecai/gists{/gist_id}","starred_url":"https://api.github.com/users/virtuecai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/virtuecai/subscriptions","organizations_url":"https://api.github.com/users/virtuecai/orgs","repos_url":"https://api.github.com/users/virtuecai/repos","events_url":"https://api.github.com/users/virtuecai/events{/privacy}","received_events_url":"https://api.github.com/users/virtuecai/received_events","type":"User","site_admin":false},"created_at":"2016-09-01T02:20:53Z","updated_at":"2016-09-01T02:20:53Z","body":"@xxiiaass 同问"}}
{"actor":{"display_login":"virtuecai"}}