1

对象

  • Object.is(value1,value2)

判断两个参数的值是否全等,与 === 类似,不过区别在于 Object.is(+0, -0)返回false, Object.is(NaN, NaN)返回true

    Object.is('foo','foo') //true
    Object.is({},{}) //false
  • Object.assign(target,o1,o2...)

用于对象的合并将参数中所有可枚举的属性复制到目标对象target,返回目标对象
若有相同的属性值,后面的会覆盖之前的值

const target = {a:1, b:2, c:3}
const o1 = {b:4, d:5}
const newTarget = Object.assign(target, o1)

console.log(target) //{a:1, b:4, c:3, d:5}
console.log(Object.is(target,newTarget)) //true
  • Object.keys(obj)

传入一个对象返回一个数组,包含对象中所有可遍历的属性的键名

const obj = {name: 'lil', age: 3}
console.log(Object.keys(obj)) //[ 'name', 'age' ]
  • Object.values(obj)

传入一个对象返回一个数组,包含对象中所有可遍历的属性的键值

const obj = {name: 'lil', age: 3}
console.log(Object.values(obj)) //[ 'lil', 3 ]
  • Object.entries(obj)

传入一个对象返回一个数组,包含对象中所有可遍历的属性的键值对

const obj = {name: 'lil', age: 3}
console.log(Object.entries(obj))
//[ [ 'name', 'lil' ], [ 'age', 3 ] ]
  • Object.setPrototypeOf(obj,prototype)

用来设置对象的prototype对象,返回参数对象本身,这个方法是ES6
正式推荐的设置原型对象的方法,等同于

function foo(obj,proto) {
    obj.__proto__ = proto;
    return obj
}
  • Object.getPrototypeOf(obj)

传入对象获取对象的原型对象

函数

  • rest参数

reset用来获取函数多余的参数,将多余的参数放入数组中,可调用数组的方法

function foo(...rest) {
  return rest
}
console.log(foo(1,2,3)) //[1,2,3]

数组

  • 扩展运算符

扩展运算符表示为...,可理解为rest参数的逆运算
扩展运算符将一个数组转为用逗号隔开的参数序列

const arr = [1,2,3,4]
console.log(arr) //1 2 3 4

将字符串转换为数组

console.log([...'hello']) //[ 'h', 'e', 'l', 'l', 'o' ]

合并数组

const arr1 = [1,2,3]
const arr2 = [4,5,6]
const arr3 = [...arr1,...arr2]
console.log(arr3) //[ 1, 2, 3, 4, 5, 6 ]
  • Array.from()

用于将类数组或者可遍历的对象转为数组

const obj = {'0':'a','1':'b','2':'c',length:3}
console.log(Array.from(obj)) //[ 'a', 'b', 'c' ]

console.log(Array.from('hello'))//[ 'h', 'e', 'l', 'l', 'o' ]
  • Array.of()

将一组值转换为数组,弥补了Array()的不足之处

Array.of(1,2,3) //[1, 2, 3]
Array(1,2,3) //[1, 2, 3]

Array.of(3) //[3]
Array(3) //[, , ,]
  • find()

返回数组中满足回调函数的第一个的值,全都不满足则返回undefined

const arr = [1, 2, 30, 100, 60]
const result = arr.find(item => item > 30)
console.log(result) //100
  • findIndex()

find()用法类似,返回值为满足回调函数的第一个值的下标
全都不满足则返回undefined

const arr = [1, 2, 30, 100, 60]
const result = arr.findIndex(item => item > 30)
console.log(result) //3
  • entries()

用于遍历数组,返回一个遍历器对象Object [Array Iterator]{}
可用for...of进行循环遍历

const arr = ['a', 'b', 'c']
for (let [index, item] of arr.entries()) {
    console.log(index, item)
}
//0 'a'
//1 'b'
//2 'c'
  • keys()

用法与entries()一样,区别在于keys()是对键名进行遍历而entries()
是对键值对进行遍历

const arr = ['a', 'b', 'c']
for (let index of arr.keys()) {
    console.log(index)
}
  • includes()

发方法返回布尔值,检测数组是否包含某个值

const arr = [1,2,'a',NaN]
arr.includes(1) //true
arr.includes(3) //false
arr.includes('a') //true
arr.includes(NaN) //true

Lil_tsj
4 声望0 粉丝

大三,积极找实习