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
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745254974a4618900.html
评论列表(0条)