I have been reading a bit on RequireJS and I get what it is trying to accomplish. However, there are some things that are unclear to me, maybe you guys could shed some light. I'll list down the questions to make it more readable:
Is RequireJS an implementation of AMD or uses AMD underneath? AMD is a unclear to me. From what I've read, it is an API specification so is it implemented? Can you use AMD even without a module loader like RequireJS? Or is it just a specification.
I've seen this code several times:
if ( typeof define === "function" && define.amd && define.amd.jQuery ) {
define( "jquery", [], function () { return jQuery; } );
}
What is the 'define' they are checking? A global object/function? Where did this come from? I'm confused about what define is and how it came about? Is it officially a part of JavaScript?
In a site I am reading, there is a line:
require(["mustache", "text!template.html"],
function(mustache, html) {...}
There it says: The plugin name and module name are separated by !. I thought the plugin would be mustache but from the sentence, the plugin name is text? Can you explain this syntax?
答案 0 :(得分:-1)
Programming languages like C, Python, Ruby, etc. all have mechanisms for writing code in self-contained, reusable modules. The JavaScript language has no such feature. Well, not aside from including multiple $profile = file_get_contents($detailURL);
tags on a web page, which is not a great option for various reasons. Now, if one were to add a module feature to JS, one would need: 1. to write all of one's JS code in an agreed-upon module format 2. a run-time or compile-time library capable of loading or pre-processing modules in that format. Put another way: JS + Module API + Module API library = JS with modules. AMD is the module API. RequireJS is the Module API library.
The essential goal of AMD, of which RequireJS is an implementation, is to define a standard API for writing JavaScript modules.
The AMD API is essentially a single function: <script>
. An AMD module is written assuming that this function has already been assigned to the global variable define
. Because JS has no built-in module feature, your only option is to rely on the existence of such a function a priori, in the same way you write browser code with the expectation that variables like define
and document
exist. You define an AMD module by passing identifiers for your module's dependencies and the code for defining your module to location
. define
is overloaded to receive this information in various formats, which are covered on the RequireJS API page.
Now that we have the basics out of the way:
define
, you use AMD (the define
property is sometimes used to disambiguate this from other module APIs which might use a define.amd
function). Similarly, if you see that there's a global define
variable, you might use CommonJS. Many scripts assign a global variable to module
as a fallback.window
. The other is mustache
, which is a sort of pseudo-module formed by applying the text!template.html
plugin to the file text
. The effect is the same as if you imported a regular AMD module which exports the contents of template.html
as a string.template.html
. In order to import such a script AMD-style, you need to modify its source code to conform to the AMD API. RequireJS's shim feature does this for you automatically.A competing JavaScript module API is CommonJS, which is the API that Node.js modules use. AMD is supposed to be more browser-friendly, but you might be interested in reading the Browserify project's take on JS modules, which brings up some excellent points. Browserify is essentially a competitor to RequireJS. Where RequireJS uses AMD and is a run-time tool, Browserify uses CommonJS and is a pre-compilation tool.