プロジェクトでよく使われるES 6
6811 ワード
コードとコメントを見れば分かります.
ES 6(es 2015)コードをES 5コードに変換する$npm install-g babel-cli $npm install babel-presete-env-save $babel./src/s 6.js-w--out-file./dist/s 5.js--presets env 割り当て値逆引用符代替引用符: str.includes()、str.starts Width()、str.endsWith()が正規表現マッチングに代わって 配列展開子回調地獄 Promiseはこのように書きます. async+await
ES 6(es 2015)コードをES 5コードに変換する
const breakfast = () => ['cake', 'coffee', 'apple']
let [dessert, drink, fruit] = breakfast()
console.info(dessert, drink, fruit) // 'cake' 'coffee' 'apple'
const breakfast = () => {
return {
dessert: 'cake',
drink: 'coffee',
fruit: 'apple'
}
}
let {dessert, drink, fruit} = breakfast()
console.info(dessert, drink, fruit) // 'cake' 'coffee' 'apple'
文字列`some content ${variable} some content`
// Array concat()
let breakfast = ['cake', 'coffee', 'apple']
let food = ['rice', ...breakfast]
console.info(...breakfast, food) // 'cake' 'coffee' 'apple', ["rice", "cake", "coffee", "apple"]
オブジェクト//
let food = {}
let drink = 'hot drink'
food[drink] = 'milk'
console.log(food['hot drink']) // 'milk'
let food = {}
let drink = 'hot drink'
food[drink] = 'milk'
let dessert = 'cake'
food.fruit = 'banane'
let breakfast = Object.assign({}, {dessert}, food) // {dessert}
console.log(breakfast) // {dessert: "cake", hot drink: "milk", fruit: "banane"}
セットメニューとMaplet fruit = new Set(['apple', 'banane', 'orange'])
/* , , :[...new Set(arr)] */
fruit.add('pear')
/* */
console.log(fruit.size) // 4
/* */
console.log(fruit.has('apple')) // true
/* */
fruit.delete('banana')
console.log(fruit) // Set(4) {"apple", "banane", "orange", "pear"}
/* */
fruit.forEach(item => {
console.log(item)
})
/* */
fruit.clear()
console.log(fruit) // Set(0) {}
let food = new Map()
let fruit = {}, cook = function(){}, dessert = ' '
food.set(fruit, 'apple')
food.set(cook, 'knife')
food.set(dessert, 'cake')
console.log(food, food.size) // Map(3) {{…} => "apple", ƒ => "knife", " " => "cake"} 3
console.log(food.get(fruit)) // "apple"
console.log(food.get(dessert)) // "cake"
food.delete(dessert)
console.log(food.has(dessert)) // false
food.forEach((value, key) => {
console.log(`${key} = ${value}`) // [object Object] = apple function(){} = knife
})
food.clear()
console.log(food) // Map(0) {}
Generatorfunction* calc(num){
yield num+1
yield num-2
yield num*3
yield num/4
return num
}
let cal = calc(4)
console.info(
cal.next(), // {value: 5, done: false}
cal.next(), // {value: 2, done: false}
cal.next(), // {value: 12, done: false}
cal.next(), // {value: 1, done: false}
cal.next(), // {value: 4, done: true} done:true
cal.next() // {value: undefined, done: true}
)
クラスclass Chef{
constructor(food){
this.food = food;
this.dish = [];
}
get menu(){ // (Getter must not have any formal parameters.)
console.log('getter ')
console.log(this.dish)
return this.dish
}
set menu(dish){ // (Setter must have exactly one formal parameter.)
console.log('setter ')
this.dish.push(dish)
}
cook(){
console.log(this.food)
}
}
let Gu = new Chef('vegetables');
Gu.cook() // "vegetables"
console.log(Gu.menu) // "get " []
Gu.menu = 'egg' // "setter "
Gu.menu = 'fish' // "setter "
console.log(Gu.menu); // "getter " ["egg", "fish"]
class Person {
constructor(name, birth){
this.name = name
this.birth = birth
}
intro(){
return `${this.name} ${this.birth}`
}
}
class One extends Person {
constructor(name, birth){
super(name, birth)
}
}
let z = new Person('zz', '01-09')
let x = new One('xx', '01-09')
console.log(z.intro(), x.intro()) // "zz 01-09" "xx 01-09"
// ES5
/* function Person(name, birth) {
this.name = name;
this.birth = birth;
}
Person.prototype.intro = function () {
return this.name + '\u7684\u751F\u65E5\u662F' + this.birth;
};
function One(name, birth) {
Person.apply(this, arguments);
}
One.prototype = new Person();
var z = new Person('zz', '01-09');
var x = new One('xx', '01-09');
console.log(z.intro(), x.intro()); // "zz 01-09" "xx 01-09" */
Promisefunction ajax(fn){
setTimeout(()=>{
console.log(' ')
fn()
},1000)
}
ajax(()=>{
ajax(()=>{
ajax(()=>{
ajax(()=>{
console.log(' 4')
})
console.log(' 3')
})
console.log(' 2')
})
console.log(' 1')
})
/*
:
1s: 1
2s: 2
3s: 3
4s: 4
*/
function delay(word){
return new Promise((reslove,reject) => {
setTimeout(()=>{
console.log(' ')
reslove(word)
},1000)
})
}
delay(' 1')
.then(word=>{
console.log(word)
return delay(' 2')
})
.then(word=>{
console.log(word)
return delay(' 3')
})
.then(word=>{
console.log(word)
return delay(' 4')
})
.then(word=>{
console.log(word)
})
.catch()
function delay(word){
return new Promise((reslove, reject) => {
setTimeout(()=>{
console.log(' ')
reslove(word)
},1000)
})
}
const start = async () => {
const word1 = await delay(' 1')
console.log(word1)
const word2 = await delay(' 2')
console.log(word2)
const word3 = await delay(' 3')
console.log(word3)
const word4 = await delay(' 4')
console.log(word4)
}
start()