function Person() {
this.age = 0;
setInterval(function growUp() {
this.age++;
}, 1000);
}
var p = new Person();
85
Slide 86
Slide 86 text
function Person() {
var self = this;
self.age = 0;
setInterval(function growUp() {
self.age++;
}, 1000);
}
var p = new Person();
86
Slide 87
Slide 87 text
function Person(){
this.age = 0;
setInterval(() => {
this.age++;
}, 1000);
}
var p = new Person();
87
Slide 88
Slide 88 text
Block Scoping Functions
var a = 5;
var b = 10;
if (a === 5) {
let a = 4;
var b = 1;
console.log(a); // 4
console.log(b); // 1
}
console.log(a); // 5
console.log(b); // 1
var a = 5;
var b = 10;
if (a === 5) {
(function () {
var a = 4;
b = 1;
console.log(a); // 4
console.log(b); // 1
})();
}
console.log(a); // 5
console.log(b); // 1
88
Slide 89
Slide 89 text
Template
var user = { name: 'Caitlin Potter' };
console.log('Thanks for V8, ' + user.name + '.');
var user = {name: 'Caitlin Potter'};
console.log(`Thanks for V8, ${user.name}.`);
89
Slide 90
Slide 90 text
Computed Property Names
var prefix = 'foo';
var test= {
[prefix + 'bar']: 'hello',
[prefix + 'baz']: 'world'
};
console.log(test['foobar']);
// -> hello
console.log(test['foobaz']);
// -> world
var prefix = 'foo';
var test= {};
test[prefix + 'bar'] = 'hello';
test[prefix + 'baz'] = 'world';
console.log(test['foobar']);
// -> hello
console.log(test['foobaz']);
// -> world
90
Slide 91
Slide 91 text
Destructuring Assignment
function f(x, y) {
if (y === undefined) {
y = 12;
}
return x + y;
}
f(3) === 15;
function f(x, y = 12) {
return x + y;
}
f(3) === 15;
91
Module
var math = require('lib/math');
console.log('2π = ' + math.sum(math.pi, math.pi));
import math from 'lib/math';
console.log('2π = ' + math.sum(math.pi, math.pi));
93
Slide 94
Slide 94 text
Property Method Assignment
var object = {
value: 42,
toString: function toString() {
return this.value;
}
};
var test= {
value: 42,
toString() {
return this.value;
}
};
94
Slide 95
Slide 95 text
Rest Parameters
function f(x) {
var y = [];
y.push.apply(y, arguments) &&
y.shift();
// y is an Array
return x * y.length;
}
function f(x, ...y) {
// y is an Array
return x * y.length;
}
console.log(f(3, 'hello', true) === 6);
95
Slide 96
Slide 96 text
Spread Operator
function f(x, y, z) {
return x + y + z;
}
f.apply(null, [1, 2, 3]) === 6;
function f(x, y, z) {
return x + y + z;
}
f(...[1,2,3]) === 6;
96
Slide 97
Slide 97 text
ES6 讓開發者少寫很多程式碼
97
Slide 98
Slide 98 text
ESLint
The pluggable linting utility for JavaScript and JSX
http://eslint.org/
98
Slide 99
Slide 99 text
{
"extends": "airbnb"
}
99
Slide 100
Slide 100 text
babel-eslint
ESLint using Babel as the parser.
https://github.com/babel/babel-eslint
100