资讯专栏INFORMATION COLUMN

项目中常用的ES6

Stardustsky / 1136人阅读

摘要:看代码及注释就懂了把代码转换为代码解构赋值字符串反引号代替引号代替了正则表达式匹配数组展开符利用的实现对象对象属性有属性有值集合和添加元素,但是不会添加相同的元素,利用这个特性实现数组去重元素数量是否有指定元素删除元

看代码及注释就懂了

把ES6(es2015)代码转换为ES5代码

$ npm install -g babel-cli

$ npm install babel-preset-env --save

$ babel ./src/es6.js -w --out-file ./dist/es5.js --presets env

解构赋值

</>复制代码

  1. const breakfast = () => ["cake", "coffee", "apple"]
  2. let [dessert, drink, fruit] = breakfast()
  3. console.info(dessert, drink, fruit) // "cake" "coffee" "apple"

</>复制代码

  1. const breakfast = () => {
  2. return {
  3. dessert: "cake",
  4. drink: "coffee",
  5. fruit: "apple"
  6. }
  7. }
  8. let {dessert, drink, fruit} = breakfast()
  9. console.info(dessert, drink, fruit) // "cake" "coffee" "apple"
字符串

反引号代替引号:`some content ${variable} some content`

str.includes()、str.startsWidth()、str.endsWith() 代替了正则表达式匹配

数组展开符

</>复制代码

  1. // 利用 Array 的 concat() 实现
  2. let breakfast = ["cake", "coffee", "apple"]
  3. let food = ["rice", ...breakfast]
  4. console.info(...breakfast, food) // "cake" "coffee" "apple", ["rice", "cake", "coffee", "apple"]
对象

</>复制代码

  1. // 对象属性
  2. let food = {}
  3. let drink = "hot drink"
  4. food[drink] = "milk"
  5. console.log(food["hot drink"]) // "milk"

</>复制代码

  1. let food = {}
  2. let drink = "hot drink"
  3. food[drink] = "milk"
  4. let dessert = "cake"
  5. food.fruit = "banane"
  6. let breakfast = Object.assign({}, {dessert}, food) // {dessert} 有属性有值
  7. console.log(breakfast) // {dessert: "cake", hot drink: "milk", fruit: "banane"}
集合 Set 和 Map

</>复制代码

  1. let fruit = new Set(["apple", "banane", "orange"])
  2. /* 添加元素,但是不会添加相同的元素,利用这个特性实现数组去重:[...new Set(arr)] */
  3. fruit.add("pear")
  4. /* 元素数量 */
  5. console.log(fruit.size) // 4
  6. /* 是否有指定元素 */
  7. console.log(fruit.has("apple")) // true
  8. /* 删除元素 */
  9. fruit.delete("banana")
  10. console.log(fruit) // Set(4) {"apple", "banane", "orange", "pear"}
  11. /* 遍历 */
  12. fruit.forEach(item => {
  13. console.log(item)
  14. })
  15. /* 清空 */
  16. fruit.clear()
  17. console.log(fruit) // Set(0) {}

</>复制代码

  1. let food = new Map()
  2. let fruit = {}, cook = function(){}, dessert = "甜点"
  3. food.set(fruit, "apple")
  4. food.set(cook, "knife")
  5. food.set(dessert, "cake")
  6. console.log(food, food.size) // Map(3) {{…} => "apple", ƒ => "knife", "甜点" => "cake"} 3
  7. console.log(food.get(fruit)) // "apple"
  8. console.log(food.get(dessert)) // "cake"
  9. food.delete(dessert)
  10. console.log(food.has(dessert)) // false
  11. food.forEach((value, key) => {
  12. console.log(`${key} = ${value}`) // [object Object] = apple function(){} = knife
  13. })
  14. food.clear()
  15. console.log(food) // Map(0) {}
Generator

</>复制代码

  1. function* calc(num){
  2. yield num+1
  3. yield num-2
  4. yield num*3
  5. yield num/4
  6. return num
  7. }
  8. let cal = calc(4)
  9. console.info(
  10. cal.next(), // {value: 5, done: false}
  11. cal.next(), // {value: 2, done: false}
  12. cal.next(), // {value: 12, done: false}
  13. cal.next(), // {value: 1, done: false}
  14. cal.next(), // {value: 4, done: true} 遍历完了后 done:true
  15. cal.next() // {value: undefined, done: true}
  16. )

</>复制代码

  1. class Chef{
  2. constructor(food){
  3. this.food = food;
  4. this.dish = [];
  5. }
  6. get menu(){ // 不得有参数(Getter must not have any formal parameters.)
  7. console.log("getter 取值")
  8. console.log(this.dish)
  9. return this.dish
  10. }
  11. set menu(dish){ // 必须有一个参数(Setter must have exactly one formal parameter.)
  12. console.log("setter 存值")
  13. this.dish.push(dish)
  14. }
  15. cook(){
  16. console.log(this.food)
  17. }
  18. }
  19. let Gu = new Chef("vegetables");
  20. Gu.cook() // "vegetables"
  21. console.log(Gu.menu) // "get 取值" []
  22. Gu.menu = "egg" // "setter 存值"
  23. Gu.menu = "fish" // "setter 存值"
  24. console.log(Gu.menu); // "getter 取值" ["egg", "fish"]

</>复制代码

  1. class Person {
  2. constructor(name, birth){
  3. this.name = name
  4. this.birth = birth
  5. }
  6. intro(){
  7. return `${this.name}的生日是${this.birth}`
  8. }
  9. }
  10. class One extends Person {
  11. constructor(name, birth){
  12. super(name, birth)
  13. }
  14. }
  15. let z = new Person("zz", "01-09")
  16. let x = new One("xx", "01-09")
  17. console.log(z.intro(), x.intro()) // "zz的生日是01-09" "xx的生日是01-09"
  18. // 转化为ES5的代码大概就是
  19. /* function Person(name, birth) {
  20. this.name = name;
  21. this.birth = birth;
  22. }
  23. Person.prototype.intro = function () {
  24. return this.name + "u7684u751Fu65E5u662F" + this.birth;
  25. };
  26. function One(name, birth) {
  27. Person.apply(this, arguments);
  28. }
  29. One.prototype = new Person();
  30. var z = new Person("zz", "01-09");
  31. var x = new One("xx", "01-09");
  32. console.log(z.intro(), x.intro()); // "zz的生日是01-09" "xx的生日是01-09" */
Promise

回调地狱

</>复制代码

  1. function ajax(fn){
  2. setTimeout(()=>{
  3. console.log("执行业务")
  4. fn()
  5. },1000)
  6. }
  7. ajax(()=>{
  8. ajax(()=>{
  9. ajax(()=>{
  10. ajax(()=>{
  11. console.log("执行结束4")
  12. })
  13. console.log("执行结束3")
  14. })
  15. console.log("执行结束2")
  16. })
  17. console.log("执行结束1")
  18. })
  19. /*
  20. 效果:
  21. 1s: 执行业务 执行结束1
  22. 2s: 执行业务 执行结束2
  23. 3s: 执行业务 执行结束3
  24. 4s: 执行业务 执行结束4
  25. */

Promise 这样写

</>复制代码

  1. function delay(word){
  2. return new Promise((reslove,reject) => {
  3. setTimeout(()=>{
  4. console.log("执行业务")
  5. reslove(word)
  6. },1000)
  7. })
  8. }
  9. delay("执行业务1")
  10. .then(word=>{
  11. console.log(word)
  12. return delay("执行业务2")
  13. })
  14. .then(word=>{
  15. console.log(word)
  16. return delay("执行业务3")
  17. })
  18. .then(word=>{
  19. console.log(word)
  20. return delay("执行业务4")
  21. })
  22. .then(word=>{
  23. console.log(word)
  24. })
  25. .catch()

async + await

</>复制代码

  1. function delay(word){
  2. return new Promise((reslove, reject) => {
  3. setTimeout(()=>{
  4. console.log("执行业务")
  5. reslove(word)
  6. },1000)
  7. })
  8. }
  9. const start = async () => {
  10. const word1 = await delay("执行业务1")
  11. console.log(word1)
  12. const word2 = await delay("执行业务2")
  13. console.log(word2)
  14. const word3 = await delay("执行业务3")
  15. console.log(word3)
  16. const word4 = await delay("执行业务4")
  17. console.log(word4)
  18. }
  19. start()

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/98664.html

相关文章

  • ES6-7

    摘要:的翻译文档由的维护很多人说,阮老师已经有一本关于的书了入门,觉得看看这本书就足够了。前端的异步解决方案之和异步编程模式在前端开发过程中,显得越来越重要。为了让编程更美好,我们就需要引入来降低异步编程的复杂性。 JavaScript Promise 迷你书(中文版) 超详细介绍promise的gitbook,看完再不会promise...... 本书的目的是以目前还在制定中的ECMASc...

    mudiyouyou 评论0 收藏0
  • ES6常用语法整合

    摘要:说到肯定是先介绍了,据阮一峰老师介绍到,是一个广泛使用的转码器,可以将代码转为代码,从而在现有环境执行。输出其他还有等可以查看阮一峰的入门 ES6也出来有一会时间了,他新增的语法糖也的确大大提高了开发者的效率,今天就总结一些自己用到最多的。 说到ES6肯定是先介绍Babel了,据阮一峰老师介绍到,Babel是一个广泛使用的转码器,可以将ES6代码转为ES5代码,从而在现有环境执行。这意...

    张迁 评论0 收藏0
  • React项目出现频率较高ES6语法

    摘要:学习过程中,发现无论是上的还是相关文档,语法都有大量的使用。如下使用语法来定义一个组件有几个注意点使用了的继承语法,关键字这里使用了上面说的语法方法名的简写,注意方法之间不加是构造函数,可以替代。内需要调用父类的构造函数即,否则就会报错。 学习React过程中,发现无论是github上的Demo还是React相关文档,ES6语法都有大量的使用。如果不了解一些ES6语法,很难学习下去。如...

    ZHAO_ 评论0 收藏0
  • 《从零构建前后分离web项目》:前端了解过关了吗?

    摘要:前端基础架构和硬核介绍技术栈的选择首先我们构建前端架构需要对前端生态圈有一切了解,并且最好带有一定的技术前瞻性,好的技术架构可能日后会方便的扩展,减少重构的次数,即使重构也不需要大动干戈,我通常选型技术栈会参考以下三点一提出自身业务的需求是 # 前端基础架构和硬核介绍 showImg(https://segmentfault.com/img/remote/146000001626972...

    lbool 评论0 收藏0
  • 《从零构建前后分离web项目》:前端了解过关了吗?

    摘要:前端基础架构和硬核介绍技术栈的选择首先我们构建前端架构需要对前端生态圈有一切了解,并且最好带有一定的技术前瞻性,好的技术架构可能日后会方便的扩展,减少重构的次数,即使重构也不需要大动干戈,我通常选型技术栈会参考以下三点一提出自身业务的需求是 # 前端基础架构和硬核介绍 showImg(https://segmentfault.com/img/remote/146000001626972...

    cgspine 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<