[go: up one dir, main page]

JavaScript Object Functions Cheat Sheet

Object.assign()

copies properties from one or more source objects to target object
Object.assign({ a: 1, b: 2 }, { c: 3 }, { d: 4 }) // { a: 1, b: 2, c: 3, d: 4 }
Object.assign(target, ...sources)

Object.create()

creates new object, using an existing object as the prototype
Object.create({ a: 1 }) // <prototype>: Object { a: 1 }
Object.create(proto, [propertiesObject])

Object.defineProperties()

defines new or modifies existing properties
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
Object.defineProperty({ a: 1, b: 2 }, 'a', {
  value: 3,
  writable: true
}); // { a: 3, b: 2 }
Object.defineProperty(obj, prop, descriptor)

Object.entries()

returns array of object's [key, value] pairs
Object.entries({ a: 1, b: 2 }) // [ ["a", 1], ["b", 2] ]
Object.entries(obj)

Object.freeze()

freezes an object, which then can no longer be changed
const obj = { a: 1 }
Object.freeze(obj)
obj.prop = 2 // error in strict mode
console.log(obj.prop) // 1
Object.freeze(obj)

Object.fromEntries()

transforms a list of key-value pairs into an object
Object.fromEntries([['a', 1], ['b', 2]]) // { a: 1, b: 2 }
Object.fromEntries(iterable)

Object.getOwnPropertyDescriptor()

returns a property descriptor for an own property
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
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
Object.getOwnPropertyNames({ a: 1, b: 2 }) // [ "a", "b" ]
Object.getOwnPropertyNames(obj)

Object.getOwnPropertySymbols()

array of all symbol properties
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.getPrototypeOf()

returns the prototype
const proto = { a: 1 }
const obj = Object.create(proto)
obj.b = 2 // obj = { b: 2 }
Object.getPrototypeOf(obj) // { a: 1 }
Object.getPrototypeOf(obj)

Object.is()

determines whether two values are the same value
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.isExtensible()

determines wether an object can have new properties added to it
const obj = {}
Object.isExtensible(obj) // true
Object.preventExtensions(obj)
Object.isExtensible(obj) // false
Object.isExtensible(obj)

Object.isFrozen()

determines if an object is frozen
const obj = {}
Object.isFrozen(obj) // false
Object.freeze(obj)
Object.isFrozen(obj) // true
Object.isFrozen(obj)

Object.isSealed()

determines if an object is sealed
const obj = {}
Object.isSealed(obj) // false
Object.seal(obj)
Object.isSealed(obj) // true
Object.isSealed(obj)

Object.keys()

returns array of object's enumerable property names
Object.keys({ a: 1, b: 2 }) // [ "a", "b" ]
Object.keys(obj)

Object.preventExtensions()

prevents new properties from being added to an object
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.prototype.hasOwnProperty()

returns boolean indicating whether object has the specified property
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
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
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
const obj = {}
obj.toString() // "[object Object]"
const arr = ['a', 'b']
arr.toString() // "a,b"
obj.toString()

Object.seal()

prevents new properties from being added and marks all existing properties as non-configurable
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.values()

returns array of object's own enumerable property values
Object.values({ a: 1, b: 'a'}) // [ 1, "a" ]
Object.values(obj)