> npm install jquery
var $ = require('jquery');
$.ajax({
url:'/myapi',
method:'GET'
}).success(functon(data){
// do somthing ...
});
var foo = require('package-name');
// example.js
var $ = require('jquery');
$.ajax({
url:'/myapi',
method:'GET'
}).success(functon(data) {
});
<scirpt src="js/jquery.js">
<script>
$.ajax({
url:'/myapi',
method:'GET'
}).success(functon(data) {
});
</script>
<scirpt src="js/jquery.js">
<script src="js/mySum.js">
<script>
var result = mySum(2, 3);
</script>
var mySum = function(a, b) {
return a + b;
};
// result => 5
var mySum = function(a, b) {
return a + b;
};
var $ = require('jquery');
var mySum = require('./mySum.js');
var result = mySum(2, 3);
var result = mySum(2, 3);
^
TypeError: object is not a function
var mySum = function(a, b) {
return a + b;
};
module.exports = mySum;
var $ = require('jquery');
var mySum = require('./mySum.js');
var result = mySum(2, 3);
// result => 5
var Sum = function(a, b) {
return a + b;
};
var Diff = function(a, b) {
return a - b;
};
var myFunc = {};
myFunc.mySum = Sum;
myFunc.myDiff = Diff;
module.exports = myFunc;
var result1 = myFunc.mySum(2, 3);
// result1 => 5
var result2 = myFunc.myDiff(2, 3);
// result2 => -1
var myFunc = require('./myFunc.js');
(EcmaScirpt2015)
(ES Harmony)
JavaScript has no Class (before ES6)
decrease global variables.
dependency is a big problem in front-end.
<scirpt src="js/jquery.js">
<scirpt src="js/jquery-plugin-1.js">
<scirpt src="js/jquery-plugin-2.js">
<scirpt src="js/jquery-plugin-3.js">
<script>
$('#myDiv').jqPlugin3();
</script>
if this code work well...
<scirpt src="js/jquery.js">
<scirpt src="js/jquery-plugin-1.js">
<scirpt src="js/jquery-plugin-2.js">
<scirpt src="js/jquery-plugin-3.js">
<script>
$('#myDiv').jqPlugin3();
</script>
what if jquery.js is missing... or rename...
<scirpt src="js/jquery.js">
<scirpt src="js/jquery-plugin-1.js">
<scirpt src="js/jquery-plugin-2.js">
<scirpt src="js/jquery-plugin-3.js">
<scirpt src="js/jquery-plugin-3.js">
<script>
$('#myDiv').jqPlugin3();
</script>
or missing some dependency plugin.
<scirpt src="js/jquery.js">
<scirpt src="js/jquery-plugin-3.js">
<scirpt src="js/jquery-plugin-1.js">
<scirpt src="js/jquery-plugin-2.js">
<script>
$('#myDiv').jqPlugin3();
</script>
or the dependency ordering is wrong....
<scirpt src="js/jquery.js">
<scirpt src="js/jquery-plugin-3.js">
<scirpt src="js/jquery-plugin-1.js">
<scirpt src="js/jquery-plugin-2.js">
<script src="js/myApp.js>"
<script>
$('#myDiv').jqPlugin3();
$('.foo').myApp();
</script>
or add some custom js-file
how can I know its dependency?
rename:jquery-myApp.js
jquery-pluging-3-myApp.js
var $ = require('jquery');
var jqPlugin3 = require('jquery-plugin-3');
var myApp = require('./myApp.js');
$('#myDiv').jqPlugin3();
$('.foo').myApp();
// jquery-plugin-3
var jqPlugin1 = require('jqPlugin1');
var jqPlugin2 = require('jqPlugin2');
var jqPlugin3 = function() {
jqPlugin1().doSomething();
jqPlugin2().doSomething();
}
module.exports = jqPlugin3;
// myApp.js
var $ = require('jquery');
var myApp = function() {
$().doSomething();
}
module.exports = myApp;
var $ = require('jquery');
var jqPlugin3 = require('jquery-plugin-3');
var myApp = require('./myApp.js');
$('#myDiv').jqPlugin3();
$('.foo').myApp();
<scirpt src="js/main.js">
<scirpt src="js/jquery.js">
<scirpt src="js/jquery-plugin-1.js">
<scirpt src="js/jquery-plugin-2.js">
<scirpt src="js/jquery-plugin-3.js">
<script src="js/myApp.js>"
<script>
$('#myDiv').jqPlugin3();
$('.foo').myApp();
</script>
<scirpt src="js/main.js">
before
after
HTML got superbly clear !!!
require & exports are part of CommonJS,
it works on Node.js ( back-end )
browser don't support CommonJS module pattern...
(1) using AMD ( Requre.js )
(2) compile CommonJS as front-end JavaScript.
(3) wait for ES6 in browser.