0% found this document useful (0 votes)
3 views14 pages

Objects in Javascript

The document provides an overview of objects in JavaScript, detailing their structure as collections of key-value pairs and their use in data modeling. It explains how to create, access, modify, and utilize methods within objects, as well as the differences between passing objects by reference and by value. Additionally, it covers useful object methods and includes interview questions related to object manipulation and comparison.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

Objects in Javascript

The document provides an overview of objects in JavaScript, detailing their structure as collections of key-value pairs and their use in data modeling. It explains how to create, access, modify, and utilize methods within objects, as well as the differences between passing objects by reference and by value. Additionally, it covers useful object methods and includes interview questions related to object manipulation and comparison.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

16 February 2024 17:21

CREATED BY SHIVAM BAREKAR


OBJECTS IN JAVASCRIPT
10 May 2024 10:06

Objects are a fundamental part of JavaScript, providing a way to group


related data and functions together. In JavaScript, an object is a
collection of key-value pairs, where each key is a string (or a symbol) and
each value can be any data type, including other objects. Objects can
have properties and methods, making them versatile for various use
cases.

Creating Objects :

There are several ways to create objects in JavaScript. The most


common one is using object literals:
Accessing Properties :

You can access object properties using dot notation or square bracket
notation:

Adding and Modifying Properties :

You can add new properties or modify existing ones:

Methods :

Methods in objects are functions associated with the object. They can be
invoked using the same notation as properties:
[Link]();

We can add dynamic keys in an object :

useCase: when we want to get the user name and value in react

Data Modeling :

Data modeling is the process of creating a visual representation of either


a whole information system or parts of it to communicate connections
between data points and structures. The goal is to illustrate the types of
data used and stored within the system, the relationships among these
data types, the ways the data can be grouped and organized and its
formats and attributes.

Objects are excellent for modeling real-world entities. For instance, you
might represent a car, a user, or a product as an object with properties
like color, brand, username, etc.
Interview Question :

1. Explain the difference between passing objects by reference and


by value in JavaScript. Provide an example to demonstrate each
scenario.

Sol: In JavaScript, primitive data types like numbers and strings are
passed by value, while objects are passed by reference.

Passing by value :

When passing by value, a copy of the primitive value is created and passed
to the function or assigned to a variable. Any changes made to the copy
do not affect the original value.
Passing by reference :

When passing by reference, a reference to the memory location of the


object is passed to the function or assigned to a variable. Any changes
made to the object through this reference will affect the original
object.

To avoid this behavior and create a true copy of the object, you can use
methods like [Link]() or the spread operator (...):

[Link]() is used to copy properties from one or more source


objects to a target object.
Comparison by Reference :

Two objects are equal only if they refer to the same object.
Independent objects (even if they look alike) are not equal:

JSON (JavaScript Object Notation) :

JSON is a data interchange format derived from JavaScript objects.


Objects can be easily converted to JSON and vice versa.
"this" Object :

In JavaScript, the this keyword refers to an object.

Which object depends on how this is being invoked (used or called).

The this keyword refers to different objects depending on how it is


used:

• In an object method, this refers to the object.


• Alone, this refers to the global object.
• In a function, this refers to the global object.
• In a function, in strict mode, this is undefined.
• In an event, this refers to the element that received the event.
• Methods like call(), apply(), and bind() can refer this to any object.

Note :

this is not a variable. It is a keyword. You cannot change the value of


this.
("use strict");

todo Let's check the this keyword values in an object methods


Regular Function Expression :

In this example, the greet method is defined using the "Method


Shorthand" syntax. It's a more concise way to define methods in object
literals.

Fat Arrow Function :


Objects Useful Methods :

1: [Link](): Returns an array containing the names of all


enumerable own properties of an object.

2: [Link](): Returns an array containing the values of all


enumerable own properties of an object.
3: [Link](): Returns an array containing arrays of key-value pairs
for each enumerable own property of an object.

4: [Link](): Returns a boolean indicating whether the


object has the specified property as an own property.

5: [Link](): Copies the values of all enumerable own properties


from one or more source objects to a target object.

6: [Link](): Freezes an object, preventing new properties from


being added to it and existing properties from being modified or deleted.
Interview Question – Objects :

1: What will be the output?

Interview Question - Object Manipulation :

Problem: Given an object representing a student, write a function to


add a new subject with its corresponding grade to the student's
record. Also check if the grades property is present or not?
Interview Question - Object Comparison :

Problem: Write a function that compares two objects to determine if


they have the same properties and values.

Example usage :
Interview Question - Object Transformation :

Problem: Write a function that transforms an array of an objects


into an object where the keys are the objects' ids.

You might also like