We can live with it
| Implementation | Applications | ECMAScript edition |
|---|---|---|
| SpiderMonkey | Firefox, Gecko layout engine, Adobe Acrobat | 2017 |
| V8 | Google Chrome, Node.js, Opera | 2016, features from 2017 |
| JavaScriptCore | WebKit, Safari, Qt 5 | 2017 |
| Chakra | Microsoft Edge | 5.1, features from 2015, 2016 and 2017 |
| JScript 9.0 | Internet Explorer, Trident layout engine | 5.1 |
| Nashorn | Java | 5.1 |
function runFunction(fn, data) {
return fn(data);
}var myFunc = function() {
// do something
}
const myArrowFunc = param => param * param;function sumFn() {
return function(a, b) { return a + b };
}const fs = require('fs');
fs.readFile('./myFile.txt', 'utf-8', function(err, data) {
// this is a callback, it gets executed
// once the file has been read
});// fetch.js
import axios from 'axios';
export function fetchSomething(fetch = axios) {
return fetch('/get/some/resource');
}// someModule.js
import { fetchSomething } from './fetch';
fetchSomething()
.then(/* do something */)
.catch(/* handle error */);// someOtherModule.js
import axios from 'axios';
import { fetchSomething } from './fetch';
const fetcherWithHeaders = axios({
// pass some custom config
timeout: 1000
});
fetchSomething(fetcherWithHeaders)
.then(/* do something */)
.catch(/* handle error */);>>> def add3(x):
... return x + 3
>>> def twice(f):
... return lambda x: f(f(x))
>>> g = twice(add3)
>>> g(7)
13function add3(v) {
return v + 3;
}
function twice(f) {
return function (v) {
return f(f(v));
}
}
var g = twice(add3);
console.log(g(7)); // 13>>> def add3(x):
... return x + 3
>>> def twice(f):
... return lambda x: f(f(x))
>>> g = twice(add3)
>>> g(7)
13let add3 = v => v + 3;
let twice = f => v => f(f(v));
let g = twice(add3);
console.log(g(7)); // 13const myArr = [ 1, 2, 3, 4 ];
const mappedArr = myArr.map(num => num * num);
console.log(mappedArr);
// [ 1, 4, 9, 16 ]const myArr = [ 1, 2, 3, 4 ];
const mappedArr = myArr.map(function(num) {
return num * num;
});
console.log(mappedArr);
// [ 1, 4, 9, 16 ]ES6
const myArr = [ 1, 2, 3, 4 ];
const mappedArr = myArr.map(num => num * num)
.map(num => num - 1);
console.log(mappedArr);
// [ 0, 3, 8, 15 ]const myArr = [ 5, 10, 4, 50, 3 ];
const multiplesOfFive = myArr.filter(num => num % 5 === 0);
console.log(multiplesOfFive);
// [ 5, 10, 50 ]const myArr = [ 5, 10, 4, 50, 3 ];
const multiplesOfFive = myArr.filter(function(num) {
return num % 5 === 0;
});
console.log(multiplesOfFive);
// [ 5, 10, 50 ]ES6
const myNumbers = [ 1, 2, 5 ];
const sum = myNumbers.reduce((sum, num) => sum + num, 0);
console.log(sum); // 8const myWords = [ 'These', 'all', 'form', 'a', 'sentence' ];
const sentence = myWords.reduce((res, word) => {
return res + ' ' + word
});
console.log(sentence);
// 'These all form a sentence'var newArr = [];
var myArr = [ 1, 2, 3 ];
for(var i = 0; i < myArr.length; i++) {
newArr.push(myArr[i] * 2);
}
console.log(newArr); // [ 2, 4, 6 ]const double = x => x * 2;
const doubled = myArr.map(double);
console.log(doubled); // [ 2, 4, 6 ]More about Functional programming
('../model/user')
('/lib/js/helpers')
('lodash')
var customerStore = require('store/customer');
var when = require('when');
module.exports = function (id) {
return when(id).then(customerStore.load);
};define(['store/customer', 'when'], function (customerStore, when) {
return function (id) {
return when(id).then(customerStore.load);
};
});(function (root, factory) {
// AMD
if (typeof define === 'function' && define.amd) {
define(['jquery', 'lodash'], factory);
// CommonJS
} else if (typeof exports === 'object') {
module.exports = factory(require('jquery'), require('lodash'));
// Window global
} else root.share = factory(root.jquery, root.lodash);
}(this, function ($, _) {
// Return public API
return {};
}));
import { load } from 'store/customer';
import when from 'when';
export default = function (id) {
return when(id).then(load);
};sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm
curl -sL https://deb.nodesource.com/setup_7.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt-get install nodejs
sudo apt-get install build-essential
sudo npm install -g nsudo n latestsudo n 7.10.0sudo n
node/6.9.1
ο node/7.10.0
node/0.12.7Already installed with node.js
Install package globally
npm install -g typescript
Initialize new package
npm init
Install package locally
node_modules directory
"dependencies" into
package.json
--save-dev to save into
"devDependencies"
npm install --save lodash
Install all packages in
package.json
npm install
Typical
package.json will look similar
{
"name": "typescript-starter",
"version": "1.6.0",
"license": "MIT",
"main": "build/index.js",
"scripts": {
"build": "tsc -p tsconfig.json",
"docs:html": "typedoc src/index.ts --mode file --out build/docs",
"version": "npm run build && npm publish --access restricted",
"postversion": "git push --follow-tags"
},
"dependencies": {
"tslib": "^1.6.0"
},
"devDependencies": {
"@types/node": "^8.0.4",
"typedoc": "^0.7.1",
"typescript": "^2.4.1"
}
}
Run script from package.json:
node_modules are available for script
npm run build
Remove package locally
node_modules directory
"dependencies" in the package.json
--save-dev for "devDependencies"
npm uninstall --save lodash
Link local package as global dependency (may need sudo)
npm link
Use linked package in other package
npm install --save ncbin-reader
npm link ncbin-reader
Release new version
patch part of version
major.minor.patch,
1.6.1 -> 1.6.2
major | minor | patch | premajor | preminor | prepatch | prerelease | from-git | <specific-version>
npm version patch -m "message"
Running npm version will do following tasks:
preversion script
package.json
version script
postversion script
Push new version tag to Git
git push --follow-tags
Publish new version to npm
npm publish
These tasks can be automatized in postversion script in
package.json
{
...
"scripts": {
...
"postversion": "git push --follow-tags && npm publish"
}
}