admin管理员组文章数量:1416642
I understand that define is used to define a module, and function is an anonymous function, but what does the argument 'require' in the function hold?
If I write anything in define(function(require){...})
, when will this be called? How to give a call to his anonymous function?
Please help, I am new to advanced JS.
I understand that define is used to define a module, and function is an anonymous function, but what does the argument 'require' in the function hold?
If I write anything in define(function(require){...})
, when will this be called? How to give a call to his anonymous function?
Please help, I am new to advanced JS.
Share Improve this question edited Mar 29, 2015 at 17:05 Artjom B. 62k26 gold badges135 silver badges230 bronze badges asked Oct 7, 2014 at 5:41 TehreemTehreem 4769 silver badges25 bronze badges 3- 2 Did you try to learn more about how does this work in RequireJS documentation? – Matías Fidemraizer Commented Oct 7, 2014 at 5:50
-
1
Ya, i read about RequireJS, but there they are using require(), as a function, and not as an argument. I dint understand that when its passed as an argument
function(require)
, what is actually passed in require? – Tehreem Commented Oct 7, 2014 at 6:03 - 1 possible duplicate of What is define used for in Javascript (aside from the obvious) – Artjom B. Commented Mar 29, 2015 at 15:51
2 Answers
Reset to default 6This is part of the requireJs api, it's not vanilla JS.
You can see the full docs in here: http://requirejs/docs/api.html#define
"require" in the above example is actually the "require" code, this pattern allows you to require a JS and, than only when loading the JS is pleted, load yet another dependency, but do so in the scope of the previously required file.
At large, this pattern allows you to break your app into multiple small JS files, and load them in an async way to speed up the loading process of web pages.
Some would argue that this is all going to be less needed when SPDY and HTTP2 are going to be more widely used. In any case, this is surely promotes a better modularity in the code design.
#BEST MATCHED ANSWER
//Function Definition of require()
function require(exports,require,module,__filename,__dirname) {
//Every program of js is wrap in this function
}
本文标签: requirejsWhat is exactly define(function(require)) in JavaScriptStack Overflow
版权声明:本文标题:requirejs - What is exactly define(function(require){...}) in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745254973a2650045.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论