programming

JavaScript object methods cheatsheet

// Legend: 

// ⚠️ Mutable
// ✅ Immutable

const sampleObject = {
  prop1: 'value1',
  prop2: 'value2',
};

// Static Methods

// ⚠️ Creates a new object with the specified prototype object.
Object.create(sampleObject);               
// ⚠️ Sets the prototype of an object.
Object.setPrototypeOf(sampleObject, {});   

// Non-Static Methods

// ✅ Returns true if the object has the specified property.
sampleObject.hasOwnProperty('prop1');          
// ✅ Returns true if the object is in the prototype chain of another object.
sampleObject.isPrototypeOf({});              
// ✅ Returns true if the specified property is enumerable.
sampleObject.propertyIsEnumerable('prop2');    
// ✅ Returns a string representation of the object.
sampleObject.toString();                       
// ✅ Returns the primitive value of the object.
sampleObject.valueOf();                        

// Example Usage

// ✅ Returns the prototype of an object.
Object.getPrototypeOf(newObj);    
// ✅ Returns true if the object has the specified property.
newObj.hasOwnProperty('key');     
// ✅ Returns true if the object is in the prototype chain of another object.
protoObj.isPrototypeOf(newObj);   
// ✅ Returns a string representation of the object.
newObj.toString();                

Mutable refers to data structures that can be modified or altered after creation, allowing changes to the underlying values.

Immutable implies that once a data structure is created, its values cannot be changed, providing stability and avoiding direct modifications.

Author avatar
About Authorgreenonsoftware

Searching for a mentor in any web technology like React, Cypress, Node, AWS, or anything else? Maybe you have a single problem and just want to solve it quickly so you can continue with more important work? Good news! We are a company focused on mentoring, education, and consultation aspects. In addition, we craft apps, educational materials, and tons of articles. If you're interested, here are some useful links to contact us and access our educational materials 🌋🤝