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.