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 = {
a: 1
}
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(obj, prop)
Example
const obj = {
a: 1
}
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({
a: 1,
b: 2
})
// [ "a", "b" ]
Object.getOwnPropertySymbols()
array of all symbol properties
Syntax
Object.getOwnPropertySymbols(obj)
Example
const obj = {
a: 1
}
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 = {
a: 1
}
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(value1, value2)
Example
const objA = {
a: 1
}
const objB = {
a: 1
}
Object.is(objA, objA) // true
Object.is(objA, objB) // false
Object.is('a', 'a') // true
Thanks for reading .... 👏👏
0 Comments
If you have any question please ask?