zoukankan      html  css  js  c++  java
  • es6字符串几个方法的理解

    es6字符串

    includes(text,index):查找字符字符串是否存在,返回t/f
    startsWith(text,index):在指定的index位置查找text是否存在,返回t/f
    endsWith(text,index):在指定的index(倒数)位置查找text是否存在,返回t/f
    index可写可不写,不写默认是0

    "content".includes('con',0);// t
    "content".startsWith('con',0);// t 0位索引开始就是con
    "content".endsWith('con',0);// f 倒数后面的值查不到
    

    字符串重复复制

    repeat(int):返回新的字符串,表示将字符串重复指定次数返回。
    int尽量写整数

    console.log("=".repeat(5));  // "====="
    

    字符串补齐

    padStart(int,text):在文本的左侧补全文本缺失的字符串,text是补缺的文本
    padEnd:(int,text):在文本的右侧补全文本缺失的字符串,text是补缺的文本
    实现

      "=====error====="  
    

    代码如下:

    console.log("error".padstart(10,"="))//=====error,
    链式写法
    console.log("error".padstart(10,"=").padEnd(15,"="))
    //=====error=====,
    

    理解"error".padstart(10,"=")
    error为5个长度的字符串,padstart设置的是10位,缺失的字符串=10-5,及缺失5个字符串,所以在error的前面添加5个"=",右侧则相反

    使用模板文字创建字符串

    const result = {
    	  success: ["max-length", "no-amd", "prefer-arrow-functions"],
    	  failure: ["no-var", "var-on-top", "linebreak"],
    	  skipped: ["no-extra-semi", "no-dup-keys"]
    	};
    
    //原生
    function makeList(arr) {
      const failureItems = arr.map(item => `<li class="text-warning">${item}</li>`);
      return failureItems;
    }
    //es6简写
    
    const makeList = arr =>{
    	return arr.map(item => `<li class="text-warning">${item}r</li>`)
    	}
    

    使用es6箭头函数的时,减少代码了,增加了可读性

  • 相关阅读:
    vue-cli 3.0 路由懒加载
    vue 路由拦截、axios请求拦截
    vue-cli 3.0 图片路径问题(何时使用 public 文件夹)
    vue 监听页面宽度变化 和 键盘事件
    WGS84、GCJ-02(火星坐标)、百度坐标,Web墨卡托坐标
    Java学习之道:Java项目打包发布
    ora-14550问题解决
    费氏搜寻法之算法分析与实现
    [置顶] woff格式字体怎么打开和编辑?
    C++小知识之Vector排序
  • 原文地址:https://www.cnblogs.com/yiniantt/p/14313934.html
Copyright © 2011-2022 走看看