Related Topics

JAVASCRIPT
In this example, we have a Person
constructor function that defines the name
and age
properties and the sayHello
method on its prototype. We then define an Employee
constructor function that extends Person
using the Object.create()
method. By setting Employee.prototype
to an object created from Person.prototype
, we establish the prototype chain and inheritance relationship between Person
and Employee
.
The Employee
constructor also calls Person.call(this, name, age)
to ensure that the name
and age
properties are properly initialized for Employee
instances.
As a result, objects created from the Employee
constructor (employee
in this case) inherit both the properties and methods defined in Person
through the prototype chain. Thus, calling employee.sayHello()
outputs “Hello!”.
In summary, inheritance in JavaScript leverages the prototype chain and the prototype
property to enable objects to inherit properties and methods from other objects, promoting code reuse and creating a hierarchical relationship between objects.
In this example, we check if the name
and city
properties exist in the person
object using the in
operator. It returns true
for the existing name
property and false
for the non-existing city
property.
Using the
hasOwnProperty()
method: ThehasOwnProperty()
method checks if an object has a specific property. Unlike thein
operator, it only considers properties directly defined on the object and does not include inherited properties.
In this example, hasOwnProperty()
is used to check if the name
and city
properties exist in the person
object. It returns true
for the existing name
property and false
for the non-existing city
property.
- Using the
undefined
check: You can also check if a property isundefined
to determine if it exists in an object. However, this approach may not be reliable if the property exists but has an explicitly set value ofundefined.
In this example, we check if the name
and city
properties exist in the person
object by comparing their values to undefined
.
It’s important to note that when using the in
operator or hasOwnProperty()
method, the property check includes properties inherited through the prototype chain. If you only want to check for properties directly defined on the object, hasOwnProperty()
is the recommended method.
In this example, the for...in
loop iterates over the enumerable properties of the person
object. The loop assigns each property name to the variable key
, and you can access the corresponding property value using person[key]
. The loop outputs the property names (name
, age
, city
) followed by their values.
It’s important to note that the for...in
loop does not guarantee a specific order of iteration. Also, it iterates over all enumerable properties, including properties inherited from the object’s prototype chain. To avoid iterating over inherited properties, you can use the hasOwnProperty()
method to filter the properties.
for…of loop: The
for...of
loop is used to iterate over iterable objects, such as arrays, strings, maps, sets, and other objects that implement the iterable protocol. It provides a simpler and more concise syntax for iterating over elements rather than focusing on object properties or keys.
In this example, the for...of
loop iterates over each element in the arr
array. The loop assigns each element value to the variable value
, allowing you to directly access and work with the element value. The loop outputs each element value (1
, 2
, 3
, 4
, 5
).
The for...of
loop does not iterate over object properties. Instead, it is specifically designed for iterating over iterable objects and provides a more intuitive and concise syntax for accessing the elements of such objects.
To summarize, the main differences between for...in
and for...of
loops when iterating over an object are:
for...in
loop iterates over enumerable properties, including inherited properties, and provides access to property names.for...of
loop iterates over iterable objects and provides access to the elements/values directly. It does not iterate over object properties.
In this example, the delete
operator is used to remove the age
property from the person
object. After executing delete person.age
, the person
object will no longer have the age
property.
It’s important to note a few things about the delete
operator and deleting properties in JavaScript:
Delete Operator: The
delete
operator is a unary operator that can be used to delete both properties and elements of an array. However, it does not affect variables or functions.Inherited Properties: The
delete
operator only deletes the specified property from the object itself, not from any objects in the prototype chain. If the property is inherited from a prototype object, it will still exist in the prototype and be accessible through the chain.Non-configurable Properties: If a property is marked as non-configurable (e.g., defined using
Object.defineProperty()
withconfigurable: false
), thedelete
operator will have no effect, and the property cannot be deleted.Effect on Objects vs. Effect on Variables: The
delete
operator removes a property from an object, but it does not affect variables or references to the object itself. It only removes the specific property binding within the object.
It’s generally recommended to be cautious when using the delete
operator since removing properties from objects can affect the object’s structure and behavior.
In this example, JSON.stringify()
converts the originalObj
into a JSON string representation. Then, JSON.parse()
parses the JSON string and creates a new object deepCopyObj
, which is a deep copy of originalObj
. However, be aware that this method discards any non-serializable properties or methods and loses references to functions and prototypes.
Using a Recursive Function: This approach recursively traverses the object and its nested properties, creating new copies of each property and nested object. It is more flexible and can handle complex objects with functions, prototypes, and non-serializable properties.
In this example, the deepCopy()
function recursively traverses the originalObj
, creating a new copy of each property and nested object. It handles both arrays and plain objects, ensuring a complete deep copy of the object.
It’s important to note that deep copying can be more resource-intensive, especially for large and complex objects with circular references. Additionally, objects that contain functions, prototypes, or non-serializable properties may not be completely preserved using these approaches. Therefore, it’s crucial to consider the specific requirements and characteristics of the objects you need to copy when choosing a deep copy method.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.