ES6学习笔记(3)

参考书《ECMAScript 6入门》
http://es6.ruanyifeng.com/

字符串的扩展
ES6之前只能识别\u0000 - \uFFFF 之间的字符,超过此范围,识别会出错;ES6弥补了这个错误

ES6扩展的新方法
codePointAt----"𠮷".CodePointAt(0)//返回超过\u0000 - \uFFFF 这个范围的字符的完整的unicode编码
fromCodePoint----String.fromCodePoint("𠮷") //返回超过\u0000 - \uFFFF 这个范围的unicode对应的字符
at----"𠮷".at(0)//识别超过\u0000 - \uFFFF 此范围的字符,返回正确的字符
normalize()----//用于合成字符。将字符的不同表示方法统一为同样的形式
includes()----//用于判断某字符或者字符串是否包含在字符串中,返回boolean值,如"this is test".includes('is') true-----同 "this is test".match(/is/); string.includes(substr,index) 两个参数表示string字符串从第index个字符开始往后,是否包含substr
startsWith()----//用于判断字符串是否以某字符或字符串开头,string.startsWidth(subtr,index) 两个参数表示判断string是否在第index个位置以substr开头
endsWidth()----//用于判断字符串是否以某字符或字符串结尾, string.endsWidth(substr,index) 两个参数表示前index个字符是否以substr结尾
padStart()----//string.padStart(length,singleStr)用于向字符串string头部补充singleStr直到长度等于length,如果超出则截去
padEnd()----//string.padEnd(length,singleStr)用于向字符串string尾部补充singleStr直到长度等于length,如果超出则截去
matchAll()

模板字符串
1.以反引号包围模板内容
`This is ${test}.` //test是变量,变量写在${}当中
`


      
  • ${a}
\` //可以任意换行,所有的空格和缩进都会保留,不用像ES5模板一样用+连接换行的字符串. 2.使用\\\` 来转义 \`\`中的\` 3\. let \[x, y\] = \[1,2\];\`This is ${x+y}\`    // this is 3.   ${}中也可以放表达式以及对象调用,函数调用等 4.String.row方法会将字符串中的所有\\进行转义,无论它是否已经转义过。\\--->\\\\    \\\\---->\\\\\\\\

标签模板
let [x,y] = [1,2];
f1`this is ${x} and ${y}` 等价于f1(["this is "," and "],[1,2])
按照这个参数方法f1(["this is "," and "],[1,2])拼接成字符串的原理是
function(strArr,...paramArr){
    let output = "";
    let index;
    for(index = 0; index < paramArr.length;i++){
        output += strArr[index] + paramArr[index]
    }
    output += strArr[index];
    return output;
}

评论

暂无评论