by Kiril Knysh
utils.js
App.UTILS = (function () {
var utils = {};
var STATE = {
NAME_PREFIX: 'Sir ',
SAL_PREFIX: '€'
};
utils.getName = function (actor) {
return STATE.NAME_PREFIX + $.trim(actor.name);
}
utils.getSalary = function (actor) {
return STATE.SAL_PREFIX + actor.salary;
}
return utils;
})();
utils/name.js
(function (utils, $) {
var STATE = {
PREFIX: 'Sir '
};
utils.getName = function (actor) {
return STATE.PREFIX + $.trim(actor.name);
}
})(App.UTILS = App.UTILS || {}, window.jQuery);
utils/salary.js
(function (utils, $) {
var STATE = {
PREFIX: '€'
};
utils.getSalary = function (actor) {
return STATE.PREFIX + actor.salary;
}
})(App.UTILS = App.UTILS || {}, window.jQuery);
utils.js
var $ = require('jQuery');
var STATE = {
NAME_PREFIX: 'Sir ',
SAL_PREFIX: '€'
};
function getName(actor) {
return STATE.NAME_PREFIX + $.trim(actor.name);
}
function getSalary(actor) {
return STATE.SAL_PREFIX + actor.salary;
}
module.exports.getName = getName;
module.exports.getSalary = getSalary;
app.js
var UTILS = require('utils');
UTILS.getName(actor);
utils.js
define(['jquery'] , function ($) {
var STATE = {
NAME_PREFIX: 'Sir ',
SAL_PREFIX: '€'
};
return {
getName: function (actor) {
return STATE.NAME_PREFIX + $.trim(actor.name);
},
getSalary: function (actor) {
return STATE.SAL_PREFIX + actor.salary;
}
};
});
app.js
require(['utils'], function (UTILS) {
UTILS.getName(actor);
});
index.html
<body>
...
<script data-main="main" src="bower_components/requirejs/require.js"></script>
</body>
main.js
requirejs.config({
paths: {
jquery: 'bower_components/jquery/dist/jquery'
}
});
requirejs(['scripts/app'], function(app) {
app.run();
});
build.js
({
baseUrl: ".",
name: "main",
out: "./build/main.js",
paths: {
jquery: "bower_components/jquery/dist/jquery.min"
}
})
utils.js
import $ from 'jquery';
const STATE = {
NAME_PREFIX: 'Sir ',
SAL_PREFIX: '€'
};
export function getName(actor) {
return STATE.NAME_PREFIX + $.trim(actor.name);
}
export function getSalary(actor) {
return STATE.SAL_PREFIX + actor.salary;
}
app.js
import { getName, getSalary } from 'utils';
getName(actor);
webpack.config.js
var path = require('path');
module.exports = {
entry: './scripts/app.js',
output: {
path: __dirname,
filename: 'app.bundle.js'
},
module: {
loaders: [
{ test: path.join(__dirname, 'scripts'), loader: 'babel-loader' }
]
}
};
utils.js
export const SEC_IN_MINUTE = 60;
export function getName(actor) {
return actor.name || '';
}
export class Actor(name, salary) {
this.name = name;
this.salary = salary;
}
app.js
import { SEC_IN_MINUTE, getName } from 'utils';
import * as UTILS from 'utils';
const actor = new UTILS.Actor('Will Smith', 2000000);
super-sort.js
export default function (array, predicate) {
//super sort algorithm here
} //no semicolon
Actor.js
export default class (name, salary) {
this.name = name;
this.salary = salary;
} //no semicolon
app.js
import superSort from 'super-sort';
import SuperActor from 'Actor';
Actor.js
export default class (name, salary) {
this.name = name;
this.salary = salary;
}
export function getName(actor) {
return actor.name || '';
}
app.js
import Actor, { getName as getActorName } from 'Actor';
inline.js
export const SEC_IN_MINUTE = 60;
export function getName(actor) {
return actor.name || '';
}
clause.js
const SEC_IN_MINUTE = 60;
function getName(actor) {
return actor.name || '';
}
export { SEC_IN_MINUTE, getName as getActorName };
utils.js
export function getSalary(actor) {
return actor.salary || -1;
}
export default function(actor) {
return actor.name || '';
}
app.js
import getName from 'utils';
import * as actorUtils from 'utils';
import { getSalary as getActorSalary } from 'utils';
utils.js
export function getSalary(actor) {
return actor.salary || -1;
}
export default function(actor) {
return actor.name || '';
}
app.js
import getName from 'utils';
import getName, * as actorUtils from 'utils';
import getName, { getSalary as getActorSalary } from 'utils';
utils.js
export function getSalary(actor) {
return actor.salary || -1;
}
export default function(actor) {
return actor.name || '';
}
app.js
import 'utils';
Actor.js
export default class (name, salary) {
this.name = name;
this.salary = salary;
}
app.js
const mFreeman = new Actor('Morgan Freeman', 3000000);
import Actor from 'Actor';
visit-utils.js
export let visits = 0;
export function visitorDetected() {
visits++;
}
app.js
import { visits, visitorDetected } from 'visit-utils';
console.log(visits); //0
visitorDetected();
console.log(visits); //1
System.import('some_module')
.then(some_module => {
// Use some_module
})
.catch(error => {
···
});
System.module(source, options?)
System.set(name, module)
System.define(name, source, options?)