最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

requirejs - What is exactly define(function(require){...}) in JavaScript - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 6

This 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 
}
发布评论

评论列表(0)

  1. 暂无评论