ES6新特性总的来说,其实是为了让程序员更方便快捷的去书写代码和逻辑,说的高大上点就是:语法糖。这篇文章,只总结了目前我个人认为在写代码时,比较喜欢常用到的一些新特性。
1、forEach 数组遍历
let array = ['a', 'b', 'c']; array.forEach(function(item, index){ console.log('值:', item, '下标', index); }); /*运行结果值: 值: a 下标 0 值: b 下标 1 值: c 下标 2*/1、字符串模板 反单引号 比如: 反单引号内: 文字描述${变量}
let temp = '测试' console.log(`temp值为:${temp}`); /*运行结果 temp值为:测试 备注:上面的写法等于:'temp值为:'+temp */2、startsWith()和startsEnd()函数,返回布尔值,表示参数字符串是否在源字符串的头部/尾部。例如:
let str = 'https://www.baidu.com/'; if(str.startsWith('http://') || str.startsWith('https://')){ console.log('这是一个网址'); }else{ console.log('这不是一个网址'); } /*运行结果 这是一个网址 */1、JSON.stringify(),一般用于我们向服务器传输数据时使用,将一个JSON对象转化为字符串。 2、JSON.parse(),作用刚好与stringify()相反,用于将一个标注的JSON字符串转化为JSON对象。 例如:
let json = {name: 'ximing', age: 18}; let temp1 = JSON.stringify(json); let temp2 = JSON.parse(temp1); console.log(json, temp1, temp2, typeof(json), typeof(temp1), typeof(temp2));运行结果(在谷歌浏览器F12调试模式下截图):
1、请看我的另一篇博客:https://blog.csdn.net/Mr_JiaTao/article/details/103399854
1、**求幂,例如:
let temp1 = 2**2; let temp2 = 4**0.5; console.log(temp1, temp2); /*运行结果 4 2 */至此,结束。