JavaScript Object Functions Flashcards

Study general JS knowledge for interviews

1
Q

What is an Object?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are enumerables in Javascript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Object.defineProperties()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the two ways Object.defineProperties() is different than assigning properties with dot notation?

A
  1. 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.
  2. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Object.keys()

A

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().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Object.values()

A

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’ ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Object.entries()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Object.fromEntries()

A

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().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Object.assign()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Object.freeze()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Object.isFrozen()

A

Object.isFrozen() determines if an object is frozen.

const obj = {}
Object.isFrozen(obj) // false

Object.freeze(obj)

Object.isFrozen(obj) //true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Object.seal()

A

Object.seal() is a built-in function that seals an object, preventing the addition or removal of properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How is Object.seal() different from Object.freeze()?

A
  1. Object.freeze() is more restrictive than Object.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.
  2. Object.freeze() affects all levels of an object’s properties, while Object.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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Object.isSealed()

A

Object.isSealed() determines if an object is sealed.

const obj = {}
Object.isSealed(obj) // false

Object.seal(obj)

Object.isSealed(obj) // true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Object.create()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Object.getPrototypeOf()

A

As Object.create() creates a new object with the specified prototype, Object.getPrototypeOf() returns the prototype of an object. Therefore, we can say that Object.getPrototypeOf() is the reverse of Object.create()

const proto = { a: 1 }
const obj = Object.create(proto)

obj.b = 2 // obj = { b: 2 }
Object.getPrototypeOf(obj) // { a: 1 }
17
Q

Object.getOwnPropertyDescriptor()

A

Object.getOwnPropertyDescriptor() is a built-in function in JavaScript that returns an object containing the descriptor for a specific property of an object. The descriptor provides information about the property, such as whether it is writable or enumerable.

There’s a Object.getOwnPropertyDescriptors() method too, it works similar way for similar use cases just that it can define multiple properties of the object at a time.

18
Q

Object.preventExtensions()

A

Object.preventExtensions() is a built-in function in JavaScript that prevents new properties from being added to an object.

const obj = { a: 1 }
Object.preventExtensions(obj)

Object.defineProperty(obj, 'b', { value: 2 }) //throws an error
19
Q

Object.isExtensible()

A

Object.isExtensibles() is a built-in function in JavaScript that** determines whether an object can have new properties added to it.**

const obj = {}
Object.isExtensible(obj) // true

Object.preventExtensions(obj)

Object.isExtensible(obj) // false
20
Q

Object.prototype.hasOwnProperty()

A

Object.prototype.hasOwnProperty() is a built-in function in JavaScript that returns boolean indicating whether an object has the specified property.
~~~
const obj = { a: 1 }

obj.hasOwnProperty(‘a’) // true
obj.hasOwnProperty(‘b’) // false
~~~

21
Q

Object.prototype.isPrototypeOf()

A

Object.prototype.isPrototypeOf() is a built-in function in JavaScript that checks if an object exists in another object’s prototype chain
~~~
const proto = { a: 1 }

const obj = Object.create(proto)
proto.isPrototypeOf(obj) // true
~~~

22
Q

Object.prototype.propertyIsEnumerable()

A

Object.prototype.propertyIsEnumerable() is a built-in function in JavaScript that checks if object exists in another object’s prototype chain.

const obj = { a: 1 }
const arr = [‘a’]

obj.propertyIsEnumerable(‘a’) // true
arr.propertyIsEnumerable(0) // true
arr.propertyIsEnumerable(‘length’) // false