JavaScript Object Functions Flashcards
Study general JS knowledge for interviews
What is an Object
?
Objects are essential for creating complex data structures and organizing code in Javascript and are the building blocks of any JavaScript program. They allow developers to perform various operations on objects, including adding or removing properties, looping through object properties, and checking for the existence of properties.
What are enumerables
in Javascript?
In JavaScript, enumerable is a property attribute that determines whether a property can be** iterated** over in a for…in
loop.
If a property is enumerable, it will be listed when you loop through an object’s properties using a for…in
loop. The default value of the enumerable property is true
.
Any property you define on an object is enumerable by default unless you specify otherwise.
Object.defineProperties()
Object.defineProperties()
is a built-in JavaScript function that allows you to define multiple properties for an object at once. This function takes** two arguments: ** the object you want to define the properties for, and an object that defines the properties.
There’s a Object.defineProperty()
method too, it works similar way for similar use cases just that it can define a **single **property at a time.
What are the two ways Object.defineProperties()
is different than assigning properties with dot notation?
-
Object.defineProperties()
allows you to define multiple properties at once, whereas assigning key and values with dot notation **can only set one property at a time. -
Object.defineProperties()
provides *greater control *over the behavior of properties. For example, you can set properties to be read-only or non-enumerable, which is not possible with dot notation.
Object.keys()
Object.keys()
is a built-in JavaScript function that returns an array of a given object’s property names.
Object.keys()
can be useful when you need to loop through an object’s properties or access a specific property by name. It’s also a handy way to* check if an object has any properties at all*, as the **returned array will be empty **if the object has **no enumerable **properties.
There’s a method Object.getOwnPropertyNames()
which returns an array of all the object’s property names, regardless of whether they are enumerable or not. This includes properties created using Object.defineProperty()
.
Object.values()
Object.values()
is a built-in function in JavaScript that allows you to extract the values of an object’s properties and return them in an array.
const myObj = { name: “Sarvesh”, age: 24, city: “Mumbai” };
const values = Object.values(myObj); // returns [ ‘Sarvesh’, 24, ‘Mumbai’ ]
Object.entries()
Object.entries()
method is a built-in function in JavaScript that allows you to get an array of all the property key-value pairs of an object. The returned array contains sub-arrays, where each sub-array represents a property of the object and consists of two elements: the property name (key) and the corresponding value.
Object.fromEntries()
Object.fromEntries()
is a built-in JavaScript function that creates an object from an array of key-value pairs. It takes an iterable (such as an array) containing key-value pairs and returns a new object with those pairs as properties. So basically, it’s just the reverse of Object.entries()
.
Object.assign()
Object.assign()
is a way to combine the properties of different objects into a new object without changing the original objects.
This function is useful when you need to merge two or more objects into a single object.
Object.freeze()
Object.freeze()
is a built-in function that freezes an object,** preventing any modifications to its properties**. This function is useful when you want to prevent accidental changes to an object.
Object.isFrozen()
Object.isFrozen()
determines if an object is frozen.
const obj = {} Object.isFrozen(obj) // false Object.freeze(obj) Object.isFrozen(obj) //true
Object.seal()
Object.seal()
is a built-in function that seals an object, preventing the addition or removal of properties.
How is Object.seal()
different from Object.freeze()
?
-
Object.freeze()
is more restrictive thanObject.seal()
When an object is frozen, its properties cannot be added, deleted, or modified in any way. When an object is sealed, its properties can still be modified, but they cannot be added or deleted. -
Object.freeze()
affects all levels of an object’s properties, whileObject.seal()
only affects the object’s immediate properties. This means that if an object has nested objects as properties,Object.freeze()
**will prevent changes to those nested objects **as well.
In layman’s language, freezing an object means that its properties cannot be changed, while **sealing **an object only prevents the addition or deletion of properties.
Object.isSealed()
Object.isSealed()
determines if an object is sealed.
const obj = {} Object.isSealed(obj) // false Object.seal(obj) Object.isSealed(obj) // true
Object.create()
Object.create()
is a built-in function in JavaScript that creates a new object with the specified prototype object and properties.
In this example, we create an object person with three properties: name, age, and greet. We then create a new object john using Object.create(person)
, which sets person as the prototype
object for john. This means that john inherits all the properties and methods of person.
We then set the name
and age
properties of john
, and call the greet
method, which outputs a message using the name
and age
properties of the john object
.