es 6,es 7,es 8文法まとめ===>(不定期更新)
3590 ワード
ES 6
1.var let const
構造配列:
1.includes
1.object.entries()
1.var let const
let,const ,
const
2.矢印関数 this
var self = this
,this , this this
3.文字列includes:
const string = 'food';
const substring = 'foo';
console.log(string.includes(substring));
。
string.repeat(str,count)
string.length < count str count == string.length
4.テンプレート文字列 const name = 'Tiger';
const age = 13;
console.log(`My cat is named ${name} and is ${age} years old.`);
5.解凍構造配列:
let [a, b, c, d] = [1, 2, 3, 4];
console.log(a);
console.log(b);
構造オブジェクト: var luke = { occupation: 'jedi', father: 'anakin' };
var occupation = luke.occupation;
var father = luke.father;
-------------------------------------------------------------
let luke = { occupation: 'jedi', father: 'anakin' };
let {occupation, father} = luke;
console.log(occupation);
console.log(father);
6.モジュール :
function sumThree(a, b, c) {
return a + b + c;
}
export { sumThree };
:
import { sumThree } from 'math/addition';
7.パラメータes6 :
function addTwoNumbers(x=0, y=0) {
return x + y;
}
8.restパラメータ :
function logArguments(...args) {
for (let arg of args) {
console.log(arg);
}
}
9.展開操作 :
Math.max(...[-1, 100, 9001, -32]);
let cities = ['San Francisco', 'Los Angeles'];
let places = ['Miami', ...cities, 'Chicago']; // ['Miami', 'San Francisco', 'Los Angeles', 'Chicago']
10.クラス :
class Person {
constructor(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
incrementAge() {
this.age += 1;
}
}
11.Maps
let map = new Map();
map.set('name', 'david');
map.get('name');
map.has('name');
12.Promises ,
func1(value1)
.then(func2)
.then(func3)
.then(func4)
.then(func5, value5 => {
});
13.Generators
function* genFunc() {
// (A)
console.log('First');
yield; //(B)
console.log('Second'); //(C)
}
ES 71.includes
:
let array = ['1','2','3']
if(array.includes('2')){
console.log(' ')
}
2.指数操作子2**3 == 8
ES 81.object.entries()
:
let obj = {a: 1, b: 2, c: 3};
Object.entries(obj).forEach(([key, value]) =>{
console.log(key + ": " + value); // a: 1, b: 2, c: 3
})
2.Async Await
:
async fetchData(query) =>{
try {
const response = await axios.get(`/q?query=${query}`);
const data = response.data;
return data;
}
catch (error) {
console.log(error)
}
}
fetchData(query).then(data => {
this.props.processfetchedData(data)
})