RequireJS library definition explanation

I started reading several manuals about RequireJS. In none of these have the keyword "define" been satisfactorily explained to me. Can someone help me with the following:

define( ["Models/Person", "Utils/random", "jquery"], function (Person, randomUtility, $) {..} ) 

What is β€œdefine”? Defines a function with an array and an anonymous function inside? Or something else? Can someone give me more information about these definitions?

Addition: Thank you, nnnnnn and pradeek for your answers. Here in Europe, it was 2:30 in the night when I sent a question. Maybe that's why I did not find out that it was a simple function call.

+44
javascript requirejs
Dec 02 '11 at 1:30
source share
4 answers

define not specific to RequireJS; it is part of the AMD specification . Burke noted that RequireJS does not implement exactly how AMD points it out, since AMD does not really consider browsers.

define does not have an anonymous function in it. define is a method available for AMD-based JavaScript files to load their data. Libraries such as RequireJS make this available to you. The specific implementation is probably not valuable to you. Therefore, I will consider the one you provided as the most common way to declare a module.

define( [array] , object );

An array is a list of modules that this module depends on. Between modules and files there is from 1 to 1. You cannot have several modules in a file and several files for one module.

An object is a module that you define. It can be anything, a structure, or a function that returns a structure. Read more in the RequireJS docs .

If the object is a function, the arguments passed to the function are the modules listed as dependencies in the first argument of the definition. It is also important to note that when you pass a function as an object , it will only work once. The methods or properties created in this instance may be available at any time, but other modules may be available to them that list this module as a dependency.

Good luck, I recommend playing around with this and reading the docs when something doesn't make sense. RequireJS documents are great for quickly using AMD modules.

+57
Dec 03 2018-11-12T00:
source share

I found define , defined at the bottom of require.js (I was also interested in what the word define , and this is the answer I was looking for):

 /** * The function that handles definitions of modules. Differs from * require() in that a string for the module should be the first argument, * and the function to execute after dependencies are loaded should * return a value to define the module corresponding to the first argument's * name. */ define = function (name, deps, callback) { var node, context; //Allow for anonymous modules if (typeof name !== 'string') { //Adjust args appropriately callback = deps; deps = name; name = null; } //This module may not have dependencies if (!isArray(deps)) { callback = deps; deps = null; } //If no name, and callback is a function, then figure out if it a //CommonJS thing with dependencies. if (!deps && isFunction(callback)) { deps = []; //Remove comments from the callback string, //look for require calls, and pull them into the dependencies, //but only if there are function args. if (callback.length) { callback .toString() .replace(commentRegExp, '') .replace(cjsRequireRegExp, function (match, dep) { deps.push(dep); }); //May be a CommonJS thing even without require calls, but still //could use exports, and module. Avoid doing exports and module //work though if it just needs require. //REQUIRES the function to expect the CommonJS variables in the //order listed below. deps = (callback.length === 1 ? ['require'] : ['require', 'exports', 'module']).concat(deps); } } //If in IE 6-8 and hit an anonymous define() call, do the interactive //work. if (useInteractive) { node = currentlyAddingScript || getInteractiveScript(); if (node) { if (!name) { name = node.getAttribute('data-requiremodule'); } context = contexts[node.getAttribute('data-requirecontext')]; } } //Always save off evaluating the def call until the script onload handler. //This allows multiple modules to be in a file without prematurely //tracing dependencies, and allows for anonymous module support, //where the module name is not known until the script onload event //occurs. If no context, use the global queue, and get it processed //in the onscript load callback. (context ? context.defQueue : globalDefQueue).push([name, deps, callback]); }; 
+4
Nov 11 '13 at 18:52
source share

I found this page. Why AMD? very helpful. To summarize this page, the AMD spec helps overcome the "write a bunch of script tags with implicit dependencies that you must manually order." This is useful when loading dependencies before executing the required functions, similar to import in other programming languages ​​such as python. AMD also prevents the global problem of namespace pollution. Check the "It is an improvement over the web current "globals and script tags" because" section.

+1
Aug 28 '13 at 13:18
source share

I think the requirements specification API JJ sums up well:

If the module has dependencies, the first argument should be an array of dependency names, and the second argument should be a definition function. The function will be called to determine the module after loading all the dependencies. The function should return an object that defines the module.

They list examples of all the various syntactic forms of definitions.

0
Jun 16 '15 at
source share



All Articles