JavaScript Object Functions Part 3
Object.isExtensible()
Object.isExtensible() determines whether an object can have new properties added to it
Syntax
Object.isExtensible(obj)
Example
const obj = {}
Object.isExtensible(obj) // true
Object.preventExtensions(obj)
Object.isExtensible(obj) // false
Object.isFrozen()
Object.isFrozen() determines if an object is frozen
Syntax
Object.isFrozen(obj)
Example
const obj = {}
Object.isFrozen(obj) // false
Object.freeze(obj)
Object.isFrozen(obj) // true
Object.isSealed()
Object.isSealed() determines if an object is sealed
Syntax
Object.isSealed(obj)
Example
const obj = {}
Object.isSealed(obj) // false
Object.seal(obj)
Object.isSealed(obj) // true
Object.keys()
Object.keys() returns array of object's enumerable property names
Syntax
Object.keys(obj)
Example
Object.keys({ a: 1, b: 2 }) // [ "a", "b" ]
Object.preventExtensions()
Object.preventExtensions() prevents new properties from being added to an object
Syntax
Object.preventExtensions(obj)
Example
const obj = {
a: 1
}
Object.preventExtensions(obj)
Object.defineProperty(obj, 'b', {
value: 2
}) // Error: Can't define property
//"b": Object is not extensible
Object.prototype.hasOwnProperty()
Object.prototype.hasOwnProperty() returns boolean indicating whether object has the specified property
Syntax
obj.hasOwnProperty(prop)
Example
const obj = {
a: 1
}
obj.hasOwnProperty('a') // true
obj.hasOwnProperty('b') // false
Object.prototype.isPrototypeOf()
Object.prototype.isPrototypeOf() checks if object exists in another object's prototype chain
Syntax
prototypeObj.isPrototypeOf(object)
Example
const proto = {
a: 1
}
const obj = Object.create(proto)
proto.isPrototypeOf(obj) // true
Object.prototype.propertyIsEnumerable()
Object.prototype.propertyIsEnumerable() checks whether the specified property is enumerable and is the object's own property.
Syntax
obj.propertyIsEnumerable(prop)
Example
const obj = {
a: 1
}
const arr = ['a']
obj.propertyIsEnumerable('a') // true
arr.propertyIsEnumerable(0) // true
arr.propertyIsEnumerable('length') // false
Object.prototype.toString()
Object.prototype.toString() returns a string representing the object
Syntax
obj.toString()
Example
const obj = {}
obj.toString() // "[object Object]"
const arr = ['a', 'b']
arr.toString() // "a,b"
Object.seal()
Object.seal() prevents new properties from being added and marks all existing properties as non-configurable
Syntax
Object.seal(obj)
Example
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.values()
Object.values() returns array of object's own enumerable property values
Syntax
Object.values(obj)
Example
Object.values({ a: 1, b: 'a'}) // [ 1, "a" ]
Thanks for reading... 👏👏
0 Comments
If you have any question please ask?