类似数组
只要是部署了Iterator接口的数据结构,Array.from都能将其转为数组。
let arr={
'0':'a',
'1':'b',
'2':'c',
length:3
}
let b=[].slice.call(arr)
console.log(b)
//['a','b','c']
//es6:
Array.from(arr)
--------------------
Array.from('hello')
// ['h', 'e', 'l', 'l', 'o']
let namesSet = new Set(['a', 'b'])
Array.from(namesSet) // ['a', 'b']
- …扩展运算符
//...运算符将某些数据结构转换成数组
[...document.querySelectorAll('div')]
[...arguments]
Array.from的接受第二个参数相当于map方法
let spans = document.querySelectorAll('span.name');
// map()
let names1 = Array.prototype.map.call(spans, s => s.textContent);
// Array.from()
let names2 = Array.from(spans, s => s.textContent)
Array.of()将一组值转成数组
弥补Array()的不足
Array() // []
Array(3) // [, , ,]
Array(3, 11, 8) // [3, 11, 8]
嵌入Object.is方法
if (!Object.is) {
Object.is = function(x, y) {
// SameValue algorithm
if (x === y) { // Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
return x !== 0 || 1 / x === 1 / y;
} else {
// Step 6.a: NaN == NaN
return x !== x && y !== y;
}
};
}
console.log(Object.is(0,-0))//false
console.log(0==-0)//true
console.log(0===-0)//true
find()
数组实例的find()和findIndex()
找出第一个符合条件的