Presented by: Ian McPhail
// Object Literal
var MyModule = {
property: 'someValue',
method: function () { }
};
// Module Pattern
var Module = (function () {
// Private state
var privateVar,
privateMethod;
privateMethod = function() { };
// Public state
return {
publicVar: 'someValue',
publicMethod: function() { }
};
})();
// app.js
var App = {};
// Code goes here
window.App = App;
// cart.js
function Cart() { }
App.Cart = new Cart();
// overlay.js
function Overlay() { }
App.Overlay = new Overlay();
// app.js
require(['lib/jquery', './cart', './overlay'], function($, Cart, Overlay) {
// Code goes here
});
// cart.js
define(['lib/jquery'], function($) {
function Cart() { }
return new Cart();
});
// overlay.js
define(['lib/jquery'], function($) {
function Overlay() { }
return new Overlay();
});
// app.js
var $ = require('jquery');
var Cart = require('./cart');
var Overlay = require('./overlay');
// Code goes here
// cart.js
var $ = require('jquery');
function Cart() { }
module.exports = new Cart();
// overlay.js
var $ = require('jquery');
function Overlay() { }
module.exports = new Overlay();
// app.js
import $ from 'lib/jquery';
import Cart from './cart';
import Overlay from './overlay';
// Code goes here
// cart.js
import $ from 'lib/jquery';
function Cart() { }
export default new Cart()
// overlay.js
import $ from 'lib/jquery';
function Overlay() { }
export default new Overlay()