🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
#### Array Array.of:解决new Array只传入一个参数n时生成的是n长度的数组的问题 ``` var a = new Array(3); a.length; //3 a; //[empty x 3]; var b = Array.of(3); b.length; //1 b; //[3] var c = Array.of(1, 2, 3); c; //[1, 2, 3] ``` Array.from:将伪数组变成数组 ``` function toArray() { return Array.prototype.slice.call(arguments); } toArray(1, 2); //[1, 2] function toArray() { return Array.from(arguments); } toArray(1, 2); //[1, 2] Array.from({length: 4, 2: "foo"}); //[undefined, undefined, foo, undefined] Array.from({length: 2}); //[undefined, undefined] //深拷贝 var a1 = [1, 2]; var a2 = Array.from(a1); a1 === a2; //false var arrLike = {length: 4, 2: "foo"}; Array.from(arrLike, (val, index) => { if(typeof val === 'stirng') return val.toUpperCase(); else return index; }); //[0, 1, "FOO", 3] ``` find:找到数组中第一个符合条件的值, finIndex:找到数组中第一个符合条件的值的索引 ``` //无论是find还是findIndex,与indexOf不同的是它们都是==而不是=== [1, 4, -5, 10].find((n) => n < 0); //-5 [1, 4, -5, 10].findIndex((n) => n < 0); //2 ``` entries\values\keys ``` var a = [1, 2, 3]; [...a.values()]; //[1, 2, 3] [...a.keys()]; //[0, 1, 2] [...a.entries()]; //[[0, 1], [1, 2], [2, 3]] ([[key, index]]) [...a[Symbol.iterator]()]; //[1, 2, 3] ``` #### Object Object.is ``` //还记得es5的proyfill吗 //es6 Object.is(a, b) //es5 Object.is = function(a, b) { if(a === 0 && b === 0) { return 1 / a === 1 / b //-0的情况,因为1/-为-Infinity, 0===-0为true } if(a !== a){ return b !== b //NaN的情况 } else { return a === b } } var x = NaN, y = 0, z = -0; x === x; //false y === z; //true Object.is(x, x); //true Object.is(y, z); //false ``` Object.getOwnPropertySymbols ``` var o = { foo: 2, [Symbol("bar")]: "hello world" //[Symbol("bar")]是计算属性而不是一个key数组 } Object.getOwnPropertySymbols(o); //[Symbol(bar)] ``` Object.setPrototypeOf() 行为委托 ``` var o1 = { a: 1 }; var o2 = {b: 2}; Object.setPrototypeOf(o2, o1); o2.a; //1 o2.__proto__; //{a: 1} o2 = Object.create(o1); //{} o2.b = 2; o2.__proto__; //{a: 1} ``` Object.assign(新对象,若干源对象) 源对象的不可枚举属性和非自身属性不会被复制 Object.assign是浅复制 #### Math #### Number Number.EPSION:2^-52 Number.MAX_SAFE_INTEGER:2^53 - 1 Number.MIN_SAFE_INFEGER:(-2)^53 + 1 Number.isNaN() ``` window.isNaN("NaN"); //true Number.isNaN("NaN"); //false ``` Number.isFinite() ``` var a = NaN, b = Infinity, c = "42"; Number.isFinite(a); //false Number.isFinite(b); //false Number.isFinite(c); //false ``` Number.isInteger() ``` Number.isInteger(4); //true Number.isInteger(4.2); //false Number.isInteger(4.0); //true Number.isInteger(NaN); //false Number.isInteger(Infinity); //false ``` #### 字符串 String.raw:获取字符串原本的内容 ``` var str = "bc"; String.raw`\ta${str}d\xE9`; //"\tabcd\xE9"而不是"abcde"(e是二声,键盘打不出来) ``` String.repeat ``` "foo".repeat(3); //"foofoofoo" ``` String.startsWith\endsWith\includes ``` var p = "step on no pets"; p.startsWith("step on"); //true p.startsWith("on", 5); //true p.endsWith("no pets"); //true p.endsWith("no", 10); //true "step on no"(第10个位置之前,也就是第九个位置是结束点) p.includes("on"); //true p.includes("on", 6); //false "n no pets" ```