摘要:看代碼及注釋就懂了把代碼轉(zhuǎn)換為代碼解構(gòu)賦值字符串反引號(hào)代替引號(hào)代替了正則表達(dá)式匹配數(shù)組展開符利用的實(shí)現(xiàn)對(duì)象對(duì)象屬性有屬性有值集合和添加元素,但是不會(huì)添加相同的元素,利用這個(gè)特性實(shí)現(xiàn)數(shù)組去重元素?cái)?shù)量是否有指定元素刪除元
看代碼及注釋就懂了
把ES6(es2015)代碼轉(zhuǎn)換為ES5代碼$ npm install -g babel-cli
$ npm install babel-preset-env --save
$ babel ./src/es6.js -w --out-file ./dist/es5.js --presets env
解構(gòu)賦值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"字符串
反引號(hào)代替引號(hào):`some content ${variable} some content`
str.includes()、str.startsWidth()、str.endsWith() 代替了正則表達(dá)式匹配
數(shù)組展開符// 利用 Array 的 concat() 實(shí)現(xiàn) let breakfast = ["cake", "coffee", "apple"] let food = ["rice", ...breakfast] console.info(...breakfast, food) // "cake" "coffee" "apple", ["rice", "cake", "coffee", "apple"]對(duì)象
// 對(duì)象屬性 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"}集合 Set 和 Map
let fruit = new Set(["apple", "banane", "orange"]) /* 添加元素,但是不會(huì)添加相同的元素,利用這個(gè)特性實(shí)現(xiàn)數(shù)組去重:[...new Set(arr)] */ fruit.add("pear") /* 元素?cái)?shù)量 */ 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 = "甜點(diǎn)" food.set(fruit, "apple") food.set(cook, "knife") food.set(dessert, "cake") console.log(food, food.size) // Map(3)?{{…} => "apple", ? => "knife", "甜點(diǎn)" => "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)?{}Generator
function* 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(){ // 不得有參數(shù)(Getter must not have any formal parameters.) console.log("getter 取值") console.log(this.dish) return this.dish } set menu(dish){ // 必須有一個(gè)參數(shù)(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" // 轉(zhuǎn)化為ES5的代碼大概就是 /* function Person(name, birth) { this.name = name; this.birth = birth; } Person.prototype.intro = function () { return this.name + "u7684u751Fu65E5u662F" + 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" */Promise
回調(diào)地獄
function ajax(fn){ setTimeout(()=>{ console.log("執(zhí)行業(yè)務(wù)") fn() },1000) } ajax(()=>{ ajax(()=>{ ajax(()=>{ ajax(()=>{ console.log("執(zhí)行結(jié)束4") }) console.log("執(zhí)行結(jié)束3") }) console.log("執(zhí)行結(jié)束2") }) console.log("執(zhí)行結(jié)束1") }) /* 效果: 1s: 執(zhí)行業(yè)務(wù) 執(zhí)行結(jié)束1 2s: 執(zhí)行業(yè)務(wù) 執(zhí)行結(jié)束2 3s: 執(zhí)行業(yè)務(wù) 執(zhí)行結(jié)束3 4s: 執(zhí)行業(yè)務(wù) 執(zhí)行結(jié)束4 */
Promise 這樣寫
function delay(word){ return new Promise((reslove,reject) => { setTimeout(()=>{ console.log("執(zhí)行業(yè)務(wù)") reslove(word) },1000) }) } delay("執(zhí)行業(yè)務(wù)1") .then(word=>{ console.log(word) return delay("執(zhí)行業(yè)務(wù)2") }) .then(word=>{ console.log(word) return delay("執(zhí)行業(yè)務(wù)3") }) .then(word=>{ console.log(word) return delay("執(zhí)行業(yè)務(wù)4") }) .then(word=>{ console.log(word) }) .catch()
async + await
function delay(word){ return new Promise((reslove, reject) => { setTimeout(()=>{ console.log("執(zhí)行業(yè)務(wù)") reslove(word) },1000) }) } const start = async () => { const word1 = await delay("執(zhí)行業(yè)務(wù)1") console.log(word1) const word2 = await delay("執(zhí)行業(yè)務(wù)2") console.log(word2) const word3 = await delay("執(zhí)行業(yè)務(wù)3") console.log(word3) const word4 = await delay("執(zhí)行業(yè)務(wù)4") console.log(word4) } start()
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/98664.html
摘要:的翻譯文檔由的維護(hù)很多人說,阮老師已經(jīng)有一本關(guān)于的書了入門,覺得看看這本書就足夠了。前端的異步解決方案之和異步編程模式在前端開發(fā)過程中,顯得越來越重要。為了讓編程更美好,我們就需要引入來降低異步編程的復(fù)雜性。 JavaScript Promise 迷你書(中文版) 超詳細(xì)介紹promise的gitbook,看完再不會(huì)promise...... 本書的目的是以目前還在制定中的ECMASc...
摘要:說到肯定是先介紹了,據(jù)阮一峰老師介紹到,是一個(gè)廣泛使用的轉(zhuǎn)碼器,可以將代碼轉(zhuǎn)為代碼,從而在現(xiàn)有環(huán)境執(zhí)行。輸出其他還有等可以查看阮一峰的入門 ES6也出來有一會(huì)時(shí)間了,他新增的語法糖也的確大大提高了開發(fā)者的效率,今天就總結(jié)一些自己用到最多的。 說到ES6肯定是先介紹Babel了,據(jù)阮一峰老師介紹到,Babel是一個(gè)廣泛使用的轉(zhuǎn)碼器,可以將ES6代碼轉(zhuǎn)為ES5代碼,從而在現(xiàn)有環(huán)境執(zhí)行。這意...
摘要:學(xué)習(xí)過程中,發(fā)現(xiàn)無論是上的還是相關(guān)文檔,語法都有大量的使用。如下使用語法來定義一個(gè)組件有幾個(gè)注意點(diǎn)使用了的繼承語法,關(guān)鍵字這里使用了上面說的語法方法名的簡(jiǎn)寫,注意方法之間不加是構(gòu)造函數(shù),可以替代。內(nèi)需要調(diào)用父類的構(gòu)造函數(shù)即,否則就會(huì)報(bào)錯(cuò)。 學(xué)習(xí)React過程中,發(fā)現(xiàn)無論是github上的Demo還是React相關(guān)文檔,ES6語法都有大量的使用。如果不了解一些ES6語法,很難學(xué)習(xí)下去。如...
摘要:前端基礎(chǔ)架構(gòu)和硬核介紹技術(shù)棧的選擇首先我們構(gòu)建前端架構(gòu)需要對(duì)前端生態(tài)圈有一切了解,并且最好帶有一定的技術(shù)前瞻性,好的技術(shù)架構(gòu)可能日后會(huì)方便的擴(kuò)展,減少重構(gòu)的次數(shù),即使重構(gòu)也不需要大動(dòng)干戈,我通常選型技術(shù)棧會(huì)參考以下三點(diǎn)一提出自身業(yè)務(wù)的需求是 # 前端基礎(chǔ)架構(gòu)和硬核介紹 showImg(https://segmentfault.com/img/remote/146000001626972...
摘要:前端基礎(chǔ)架構(gòu)和硬核介紹技術(shù)棧的選擇首先我們構(gòu)建前端架構(gòu)需要對(duì)前端生態(tài)圈有一切了解,并且最好帶有一定的技術(shù)前瞻性,好的技術(shù)架構(gòu)可能日后會(huì)方便的擴(kuò)展,減少重構(gòu)的次數(shù),即使重構(gòu)也不需要大動(dòng)干戈,我通常選型技術(shù)棧會(huì)參考以下三點(diǎn)一提出自身業(yè)務(wù)的需求是 # 前端基礎(chǔ)架構(gòu)和硬核介紹 showImg(https://segmentfault.com/img/remote/146000001626972...
閱讀 3547·2021-11-22 11:59
閱讀 950·2021-09-27 13:36
閱讀 3612·2021-09-24 09:47
閱讀 2260·2021-09-01 11:39
閱讀 979·2021-08-31 09:37
閱讀 2311·2021-08-05 10:01
閱讀 1673·2019-08-30 15:55
閱讀 701·2019-08-30 15:54