ES6-新增方法】的更多相关文章

ES6新增的常用数组方法 let arr = [1, 2, 3, 2, 1]; 一 forEach => 遍历数组 arr.forEach((v, i) => { console.log(v, i); }); 二 map => 使用一个数组, 利用某规则映射得到一个新数组 let mapArr = arr.map((v, i) => { return v * v; }); arr.map((v, i) => v * v); // 如果只有一句话, 可以省略大括号和return…
Es6新增对象方法的访问描述符:get(只读).set(只写),可以直接使用,一般用于数据监听,用途类似于vue.$watch. var obj = { a:1, get bar() { return this.a}, set bar(a) { this.a = a; return this.a } } obj.bar //1 obj.bar = 2 obj.bar //2…
es6新增了4个字符串处理的方法:startsWith,endsWith,includes,repeat. 1.简单使用 includes()返回布尔值,表示是否找到了参数字符串 startsWith()返回布尔值,表示参数字符串是否在源字符串的头部 endsWith()返回布尔值,表示参数字符串是否在源字符串的尾部 let str="lxy"; //字符串是否以某个字符开头 console.log(str.startsWith('l'));//true console.log(str…
es6新增的遍历数组的方法,后面都会用这个方法来遍历数组,或者对象,还有set,map let arr=[1,2,3,4,3,2,1,2]; 遍历数组最简洁直接的方法法 for (let value of arr) { console.log(value);//输出1,2,3,4,3,2,1,2 } 1. 数组.map() 返回一个新的数组,es5要复制一个新的数组我们一般用循环,现在直接用map let arr=[1,2,3,4,3,2,1,2]; let newArr=arr.map((va…
ES6新增的math,Number方法,下面总结了一些我觉得有用的 Nunber.isInteger()判断是否为整数,需要注意的是1,和1.0都会被认为是整数 console.log(Number.isInteger(1.0))//true console.log(Number.isInteger(1))//true console.log(Number.isInteger("1"))//false console.log(Number.isInteger("1.1&quo…
模板字符串 传统写法 var str = 'There are <b>' + basket.count + '</b> ' + 'items in your basket, ' + '<em>' + basket.onSale + '</em> are on sale!' ES6写法 let str = ` There are <b>${basket.count}</b> items in your basket, <em>…
1.let ES6中新增的用于声明变量的关键字. let 声明的变量只在所处于的块级有效. 注意:使用 let 关键字声明的变量才具有块级作用域,var 关键字是不具备这个特点的. 1. 防止循环变量变成全局变量. 2. 不存在变量提升 3. 暂时性死区 if(true){ let a=10; } console.log(a); // a is not defined //防止循环变量变成全局变量 for(var i=0;i<2;i++){ } console.log(i); // i=2(只有…
1. Array.from() Array.from方法用于将两类对象转为真正的数组:类数组的对象( array-like object )和可遍历( iterable )的对象(包括 ES6 新增的数据结构 Set 和Map ). let arrayLike = { '0': 'a', '1': 'b', '2': 'c', length: 3 }; // ES5 的写法 var arr1 = [].slice.call(arrayLike); // ['a', 'b', 'c'] // ES…
模板字符串 模板字符串:我理解为将字符串格式化.模板化,将字符串加强处理,此处的模板有动词的意思. 字符串模板基本格式: `xxxxxx`(前后都用反引号[tab键上面按键]引起来).除了作为普通字符串 外:还可以用来定义多行字符串:也可以在字符串中插入变量和表达式,进行字符串内容扩充和计算. 1.普通字符串: let testStr = `ES6 TestDemo`; console.log(testStr); // ES6 TestDemo 2.普通字符串 添加标签.换行符: let tes…
前端面试之ES6新增了数组中的的哪些方法?! 我们先来看看数组中以前有哪些常用的方法吧! 1 新增的方法! 1 forEach() 迭代遍历数组 回调函数中的三个参数 value: 数组中的每一个元素- index: 每一个数组元素中的索引号! arr: 数组对象本身 <script> // ES5中新增的方法 // forEach() 迭代遍历数组 var arr = [2, 3, 5]; var sum = 0; arr.forEach(function(value, index, arr…