1. String.raw

    String.raw是一个模板字符串的标签函数(如果你不晓得什么是标签函数,点⚑),它返回模板字符串的原始字符串。比如说\n不会被当成换行解决,而是被当做两个一般字符\n

    console.log(String.raw`1\n2`); //1\n2

    通常,咱们不须要把它当做一般函数来调用,然而如果你肯定要把它当做一般函数调用的话,要留神传入参数的格局:

    (template: {    raw: readonly string[] | ArrayLike<string>;}, ...substitutions: any[])=>string

    String.raw的第一个参数必须是一个有raw属性的对象,该属性值为字符数组或者字符类数组,残余参数就是插入的值。

    console.log(String.raw({ raw: 'abcd' }, 0, 1, 2)); //a0b1c2d//3 4两个插值超出了模板数组的范畴,会被疏忽console.log(String.raw({ raw: ['a', 'b', 'c', 'd'] }, 0, 1, 2, 3, 4)); //a0b1c2d
  1. String.prototype.repeat

    函数类型:

    (count?:number)=>string

    repeat函数会依据传入的参数n,返回一个原字符串反复了n次的新字符串,参数n默认为0

    //传入0或者不传参数时,原字符串反复0次,返回空字符串console.log('a'.repeat(0));// ''console.log('a'.repeat(2));// 'aa'
  2. String.prototype.startsWith,String.prototype.endsWith,String.prototype.includes

    函数类型:

    (queryString?:string,startPosition?:number)=>boolean

    startsWithendsWidthincludes函数都是ES6新增的用于查看字符串的函数,它们接管两个参数,第一个参数queryString是要在原字符串中查问的字符串,第二个参数startPostion是开始查问的地位。在includesstartsWith中,startPosition默认值为0,在endsWith中,startPostion默认为原字符串length

    console.log('abcde'.includes('a')); //true//扭转起始地位console.log('abcde'.includes('a', 1)); //falseconsole.log('abcde'.startsWith('a')); //true//扭转起始地位console.log('abcde'.startsWith('a', 1)); //falseconsole.log('abcde'.endsWith('a')); //false//扭转起始地位console.log('abcde'.endsWith('a', 1)); //true

    要留神的是,当要查看的字符串,即参数queryString空字符串''时,这三个函数的返回后果始终为true

    console.log('abcde'.includes('')); //true//扭转起始地位console.log('abcde'.includes('', 1)); //trueconsole.log('abcde'.startsWith('')); //true//扭转起始地位console.log('abcde'.startsWith('', 1)); //trueconsole.log('abcde'.endsWith('')); //true//扭转起始地位console.log('abcde'.endsWith('', 1)); //true