/

JavaScript Object 备忘单

对象方法

Object.assign()

copies properties from one or more source objects to target object

1
2
3
Object.assign({ a: 1, b: 2 }, { c: 3 }, { d: 4 }) // { a: 1, b: 2, c: 3, d: 4 } Object.assign(target, ...sources)

Object.entries()

returns array of object's [key, value] pairs

1
2
3
Object.entries({ a: 1, b: 2 }) // [ ["a", 1], ["b", 2] ] Object.entries(obj)

Object.fromEntries()

transforms a list of key-value pairs into an object

1
2
3
Object.fromEntries([['a', 1], ['b', 2]]) // { a: 1, b: 2 } Object.fromEntries(iterable)

Object.is()

determines whether two values are the same value

1
2
3
4
5
6
7
const objA = { a: 1 } const objB = { a: 1 } Object.is(objA, objA) // true Object.is(objA, objB) // false Object.is('a', 'a') // true Object.is(value1, value2)

Object.keys()

returns array of object's enumerable property names

1
2
3
Object.keys({ a: 1, b: 2 }) // [ "a", "b" ] Object.keys(obj)

Object.values()

returns array of object's own enumerable property values

1
2
3
Object.values({ a: 1, b: 'a'}) // [ 1, "a" ] Object.values(obj)

对象属性的相关方法

Object.defineProperties()

defines new or modifies existing properties

1
2
3
4
5
6
Object.defineProperties({ a: 1, b: 2 }, { a: { value: 3, writable: true, }}) // { a: 3, b: 2 } Object.defineProperties(obj, props)

Object.defineProperty()

defines new or modifies existing property

1
2
3
4
5
6
Object.defineProperty({ a: 1, b: 2 }, 'a', { value: 3, writable: true }); // { a: 3, b: 2 } Object.defineProperty(obj, prop, descriptor)

Object.getOwnPropertyDescriptor()

returns a property descriptor for an own property

1
2
3
4
const obj = { a: 1 } Object.getOwnPropertyDescriptor(obj, 'a') // { value: 1, writable: true, enumerable: true, configurable: true } Object.getOwnPropertyDescriptor(obj, prop)

Object.getOwnPropertyDescriptors()

returns all own property descriptors

1
2
3
4
const obj = { a: 1 } Object.getOwnPropertyDescriptors(obj, 'a') // { a: { value: 1, writable: true, enumerable: true, configurable: true } } Object.getOwnPropertyDescriptor(obj, prop)

Object.getOwnPropertyNames()

returns array of all properties

1
2
3
4
5
6
const obj = { a: 1 } const b = Symbol('b') obj[b] = 'someSymbol' // obj = { a: 1, Symbol(b): "symbol" } Object.getOwnPropertySymbols(obj) // [ Symbol(b) ] Object.getOwnPropertySymbols(obj)

控制对象状态的方法

Object.preventExtensions()

prevents new properties from being added to an object

1
2
3
4
5
const obj = { a: 1 } Object.preventExtensions(obj) Object.defineProperty(obj, 'b', { value: 2 }) // Error: Can't define property "b": Object is not extensible Object.preventExtensions(obj)

Object.isExtensible()

determines wether an object can have new properties added to it

1
2
3
4
5
6
const obj = {} Object.isExtensible(obj) // true Object.preventExtensions(obj) Object.isExtensible(obj) // false Object.isExtensible(obj)

Object.isSealed()

determines if an object is sealed

1
2
3
4
5
6
const obj = {} Object.isSealed(obj) // false Object.seal(obj) Object.isSealed(obj) // true Object.isSealed(obj)

Object.seal()

prevents new properties from being added and marks all existing properties as non-configurable

1
2
3
4
5
6
7
const obj = { a: 1 } Object.seal(obj) obj.a = 2 // { a: 2 } obj.b = 3 // error in strict mode delete obj.a // error in strict mode Object.seal(obj)

Object.isFrozen()

determines if an object is frozen

1
2
3
4
5
6
const obj = {} Object.isFrozen(obj) // false Object.freeze(obj) Object.isFrozen(obj) // true Object.isFrozen(obj)

Object.freeze()

freezes an object, which then can no longer be changed

1
2
3
4
5
6
const obj = { a: 1 } Object.freeze(obj) obj.prop = 2 // error in strict mode console.log(obj.prop) // 1 Object.freeze(obj)

原型链相关方法

Object.create()

creates new object, using an existing object as the prototype

1
2
3
Object.create({ a: 1 }) // <prototype>: Object { a: 1 } Object.create(proto, [propertiesObject])

Object.getPrototypeOf()

returns the prototype

1
2
3
4
5
6
const proto = { a: 1 } const obj = Object.create(proto) obj.b = 2 // obj = { b: 2 } Object.getPrototypeOf(obj) // { a: 1 } Object.getPrototypeOf(obj)

Object.setPrototypeOf()

Object 的实例方法

Object.prototype.hasOwnProperty()

returns boolean indicating whether object has the specified property

1
2
3
4
5
const obj = { a: 1 } obj.hasOwnProperty('a') // true obj.hasOwnProperty('b') // false obj.hasOwnProperty(prop)

Object.prototype.isPrototypeOf()

checks if object exists in another object's prototype chain

1
2
3
4
5
const proto = { a: 1 } const obj = Object.create(proto) proto.isPrototypeOf(obj) // true prototypeObj.isPrototypeOf(object)

Object.prototype.propertyIsEnumerable()

checks whether the specified property is enumerable and is the object's own property

1
2
3
4
5
6
7
const obj = { a: 1 } const arr = ['a'] obj.propertyIsEnumerable('a') // true arr.propertyIsEnumerable(0) // true arr.propertyIsEnumerable('length') // false obj.propertyIsEnumerable(prop)

Object.prototype.toString()

returns a string representing the object

1
2
3
4
5
6
const obj = {} obj.toString() // "[object Object]" const arr = ['a', 'b'] arr.toString() // "a,b" obj.toString()
作者:liuk123标签:js分类:javascript

本文是原创文章,采用 CC BY-NC-ND 4.0 协议, 完整转载请注明来自 liuk123