es6 常用方法

模板字符串
</head> <body> <div id="div1"> <table> <tr> <td></td> <td></td> <td></td> <td></td> </tr> </table> </div> </body> <script> // var res = '<table><tr><td>'+res.name+'</td>' // 模版字符串 反引号包裹 `` var name = '张三'; var age = 18; function fn(arr, v1, v2) { console.log(arr); // ["他叫", ",今年", "岁了。", raw: Array(3)] console.log(v1); //张三 console.log(v2); //18 } fn `他叫${name},今年${age}岁了。` //标签模版字符串 var json = { 'a': [{ 'name': 'zs', 'age': 18, 'gender': '', 'score': 90 }] } var oDiv = document.getElementById('div1'); oDiv.innerHTML = `<table><tr><td>${json.a[0].name}</td><td>${json.a[0].age}</td><td>${json.a[0].gender}</td><td>${json.a[0].score}</td></tr></table>` </script>


字符串常用方法 


// 1.repeat() 重复 var name = 'zs'; var res = name.repeat(4) console.log(name) console.log(res) // 2.includes() 表示是否包含某一项,有就返回true 没有就返回false,同时支持第二个参数,代表从第几项开始查找(从索引值开始计算) var r = 'hello'; console.log(r.includes('w')); console.log(r.includes('e')); console.log(r.includes('e',2)); // 3.startsWith() 表示是否是开始位置,同时支持第二个参数,代表从第几项开始查找(从索引值开始计算) 返回结果是true或者false var m = 'welcome'; console.log(m.startsWith('l',2)); // 4.endsWith() 表示是否是结束位置,同时支持第二个参数,针对前N个字符 返回结果是true或者false var m = 'welcome'; console.log(m.endsWith('o',5)); // 5.String.raw() 未加工 换行 console.log('hellow world') console.log(String.raw`hellow world`)
原文地址:https://www.cnblogs.com/guirong/p/13592480.html