zoukankan      html  css  js  c++  java
  • es6之函数参数默认值、字符串方法、for of

    1.假设有这么一个应用场景:点餐的时候要选择主食和饮料,假如有人忘记点了饮料,我们可以设置一个饮料的默认值

        function orderCombo(comboName = "巨无霸套餐", drink = "可口可乐") {
          console.log(`您点了${comboName},饮料是:${drink}。`)
        }
        orderCombo("巨无霸套餐") //您点了巨无霸套餐,饮料是:可口可乐。
        orderCombo("巨无霸套餐","橙汁") //您点了巨无霸套餐,饮料是:橙汁。
        // 如果我只想传第二个值不传第一个:人为设置undefined
        orderCombo(undefined,"雪碧")  //您点了巨无霸套餐,饮料是:雪碧。

    2.字符串方法:inclueds  startWith  endWith

        const string = "abc"
        const substring = "ab"
        // 判断一个字符串中是否包含另一个字符串:inculdes
        console.log(string.includes(substring)) //true
        console.log(string.includes("d")) //false
    
        // 判断一个字符串是否以一个字符开头:startWith
        console.log(string.startsWith(substring)) //true
        console.log(string.startsWith("b")) //false
        // 判断一个字符串是否在第几位以某个字符开头的
        console.log(string.startsWith("b", 1)) //true
    
        // 判断一个字符串是否以某字符结尾
        console.log(string.endsWith("c")) //true
        // endWith第二个参数是对原字符串截取之后再判断(从1开始数)
        const string1 = "abfuhrewrefoakms"
        console.log(string1.endsWith("wre",10))   //true

    3. 遍历数组:for off

    看个练习吧:

       const foods = ["香辣鸡腿堡","墨西哥鸡肉卷","麻辣烤翅"]
        for(const [index,food] of foods.entries()){
          console.log(`第${index}号套餐是${food}`)
        }
        // 第0号套餐是香辣鸡腿堡
        // 第1号套餐是墨西哥鸡肉卷
        // 第2号套餐是麻辣烤翅

    for...of并不是万能的,当对象object的时候用for...in

  • 相关阅读:
    项目经理成长之路-初入职场(二)
    项目经理成长之路-我的大学(一)
    别了郑州,2020再出发
    RPC协议实践入门
    Spark学习进度11-Spark Streaming&Structured Streaming
    使用Python自动填写问卷星(pyppeteer反爬虫版)
    All mirror URLs are not using ftp, http[s] or file.
    2018蓝桥杯A组省赛A,B,C,D
    Spark学习进度10-DS&DF基础操作
    SparkSQL学习进度9-SQL实战案例
  • 原文地址:https://www.cnblogs.com/yaya-003/p/12773356.html
Copyright © 2011-2022 走看看