I have been reading about AMD and implementations like requirejs
. Most of the resources covers the usage and API's.
But, when it comes to implementing this, how can you load a JavaScript file into a variable just like that? For example, you can see functions like this:
define(['jquery'], function($){
});
var jquery = require('./jquery');
From an API consumer's perspective, all I can understand is that the file jquery.js
has magically become $
, jQuery
etc? How is that achieved?
Any examples would be helpful.
How do AMD loaders work under the hood? is a helpful read.
Edit: I think the eval
answers below are nice because it actually is an eval
problem in some ways. But I would like to know this from the perspective of an AMD specs implementation.