Open In App

JavaScript Object Reference

Last Updated : 29 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

JavaScript Objects are the most important data type and form the building blocks for modern JavaScript. The “Object” class represents the JavaScript data types. Objects are quite different from JavaScript’s primitive data types (Number, String, Boolean, null, undefined, and symbol). It is used to store different types of key collections and complex entities. It can be created by using the Object() method or by using object initializer/literal syntax.

What are JavaScript Objects?

JavaScript objects are collections of properties, where each property is defined as a key-value pair. They are used to model real-world entities and complex data structures.

Syntax:

Objects can be created using either the object literal syntax {} or the Object() constructor.

objectName.methodName()

Example:  Using function definition as property value and accessing with additional details. 

// Object creation
let student = {
    name: "Martin",
    class: "12th",
    section: "A",

    studentDetails: function () {
        return this.name + " " + this.class
            + " " + this.section + " ";
    }
};

// Display object data
console.log("STUDENT " + student.studentDetails());

Output:

STUDENT Martin 12th A

JavaScript Complete Object Reference

1. JavaScript Object Properties

A JavaScript property is a member of an object that associates a key with a value.

1. Instance Properties:

An instance property is a property that has a new copy for every new instance of the class.

Instance Properties

Description 

Example 

constructorReturns a reference to the object constructor function that has created the instance of an object. 

1. JavaScript Object Methods

JavaScript methods are actions that can be performed on objects. 

1. Static Methods:

If the method is called using the Object class itself then it is called a static method of Object.

Static Methods

Description

Example

assign()Copy the values and properties from one or more source objects to a target object.
create()JavaScript does not have the traditional classes seen in other languages.
defineProperty()Defines a new property directly on an object and returns the object.
defineProperties()Defines object a new or modifies existing properties directly on an object and it returns the object.
entries()Return an array consisting of enumerable property [key, value] pairs of the object.
freeze()There is a method Object.freeze() which is used to freeze an object.
fromEntries()Transform a list of key-value pairs into an object.
getOwnPropertyDescriptor()Returns a property descriptor for the own property of a given object.
getOwnPropertyNames()Returns all properties that are present in a given object.
getOwnPropertySymbols()Returns an array of all symbol properties that are present in a given object.
getPrototypeOf()Check the prototype of an object that the user has created.
hasOwn()Checks if a given property exists or not 
Object.is()Determine whether two values are the same or not.
isExtensible()Checks whether an object is extensible or not.
isFrozen( )Determine if an object is frozen or not.
isSealed( )Determine if an object is sealed or not.
keys()Return an array whose elements are strings corresponding to the enumerable properties.
preventExtensions()Additionally, it prevents the prototype of the item from being changed.
seal()There is a method Object.seal() which is used to seal an object.
setPrototypeOf()The internal [[Prototype]] property of a specified object to another object or null.
toLocaleString()It returns a string representing the object.
values()Return an array whose elements are the enumerable property values found on the object.

2. Instance Methods:

If the method is called on an instance of a date object then it is called an instance method.

Instance Methods

Description

Example

defineGetter()Called when the specified property is looked up
hasOwnProperty()Check whether the object has the specified property as its own property.
isPrototypeOf()Checks if an object exists in another object’s prototype chain.
IsEnumerable()Returns a Boolean indicating whether the specified property is enumerable and is the object’s own property.

JavaScript Object Reference – FAQs

What is the difference between primitive data types and objects in JavaScript?

Primitive data types (Number, String, Boolean, null, undefined, and Symbol) are immutable and store single values, while objects are mutable and can store collections of key-value pairs. Primitives are compared by value, whereas objects are compared by reference.

How do you clone an object in JavaScript?

To clone an object in JavaScript, you can use various methods such as the spread operator {…obj}, Object.assign({}, obj), or for a deep copy, JSON.parse(JSON.stringify(obj)). Each method has different implications based on the depth and complexity of the object.

What is the purpose of the Object.defineProperty() method?

Object.defineProperty() is used to define a new property or modify an existing property on an object. It allows you to specify property descriptors like writable, enumerable, and configurable, providing more control over the property behavior.

How does prototypal inheritance work in JavaScript?

In JavaScript, objects inherit properties and methods from their prototype. Each object has a prototype (accessible via Object.getPrototypeOf(obj)), and objects can share properties and methods through this prototype chain. This mechanism is known as prototypal inheritance.

What are JavaScript object property descriptors and how are they used?

Property descriptors are attributes that define the behavior of a property. They include value, writable, enumerable, and configurable. These descriptors can be set using Object.defineProperty() or Object.defineProperties() to control how properties of an object are handled.



Next Article

Similar Reads

three90RightbarBannerImg