JavaScript Object Functions Cheat Sheet Part 2



 JavaScript Object Functions Part 1

Object.freeze()

Object.freeze() freezes an object, which then can no longer be changed.

Syntax 


  Object.freeze(obj)


Example

  
  const obj = {
    a1
  }
  Object.freeze(obj)
  obj.prop = 2 // error in strict mode
  console.log(obj.prop// 1



Object.fromEntries()

Object.fromEntries() transforms a list of key-value pairs into an object.

Syntax

 
    Object.fromEntries(iterable)


Example

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



Object.getOwnPropertyDescriptor()

Object,getOwnPropertyDescruotor() returns a property descriptor for an own property.

Syntax

  
    Object.getOwnPropertyDescriptor(objprop)


Example
  
  const obj = {
    a1
  }
  Object.getOwnPropertyDescriptor(obj'a')
  // { value: 1, writable: true, enumerable: 
  //true, configurable: true }


Object.getOwnPropertyNames()

Object.getOwnPropertyNames() returns array of all properties


Syntax
  
    Object.getOwnPropertyNames(obj)


Example
 
 Object.getOwnPropertyNames({
    a1,
    b2
  })
  // [ "a", "b" ]



Object.getOwnPropertySymbols()

array of all symbol properties

Syntax


  Object.getOwnPropertySymbols(obj)
  

Example

  const obj = {
    a1
  }
  const b = Symbol('b')
  obj[b= 'someSymbol'
  // obj = { a: 1, Symbol(b): "symbol" }
  Object.getOwnPropertySymbols(obj)
  // [ Symbol(b) ]

  

Object.getPrototypeOf()

Object.getPrototypeOf() returns the prototype

Syntax

  Object.getPrototypeOf(obj)


Example

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


Object.is()

Object.is() determines whether two values are the same value.

Syntax


  Object.is(value1value2)



Example
  
const objA = {
    a1
  }
  const objB = {
    a1
  }
  Object.is(objAobjA// true
  Object.is(objAobjB// false
  Object.is('a''a'// true
  




Thanks for reading .... 👏👏



















Reactions

Post a Comment

0 Comments