JavaScript Revision (Part-2): Advanced Object Concepts
Sameer Dixit
Software Engineer at Simplotel | JP Morgan & Chase Co. Code for Good Runner Up | I don’t just think outside the box—I design it, build it, and upgrade it to version 2.0!
In this part, we dive deeper into JavaScript objects and explore advanced concepts that will help to write more efficient and maintainable code. Let's get started!
JavaScript Revision (Part-2): Advanced Object Concepts
In this part, we dive deeper into JavaScript objects and explore advanced concepts that will help to write more efficient and maintainable code. Let's get started!
Singleton Objects:
const singleton = (function() { let instance; function createInstance() { return { name: "SingletonInstance" }; } return { getInstance: function() { if (!instance) { instance = createInstance(); } return instance; } }; })();
Object Literals:
const person = { name: "Sameer", age: 23 };
Methods of Accessing Objects:
console.log(person.name); // Dot notation
console.log(person["age"]); // Bracket notation
Symbols:
- Unique and immutable data type used to create unique property keys.
const sym = Symbol("uniqueKey");
const obj = {
[sym]: "value"
};
Preventing Object Modification:
- Object.freeze(): Prevents modification of existing properties and prevents new properties from being added.
const obj = { name: "Alice" };
Object.freeze(obj);
obj.name = "Bob"; // Will not change
Function Calls by Reference and Value:
- Passing objects by reference allows modification of the original object.
领英推荐
function modify(obj) {
obj.name = "Bob";
}
const person = { name: "Alice" };
modify(person); // person.name is now "Bob"
Using this Keyword:
- Refers to the object from which the method was called.
const person = {
name: "sameer",
greet: function() {
console.log(`Hello, my name is ${this.name}`);
}
};
person.greet(); // Hello, my name is sameer
Combining Objects:
- Object.assign() and spread operator (`...`) to merge objects.
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
const merged = Object.assign({}, obj1, obj2); // { a: 1, b: 3, c: 4 }
const spreadMerged = { ...obj1, ...obj2 }; // { a: 1, b: 3, c: 4 }
Using Object.entries, Object.keys, and Object.values:
- Object.entries(): Returns an array of a given object's key-value pairs.
const entries = Object.entries(person); // [["name", "Alice"], ["age", 30]]
- Object.keys(): Returns an array of a given object's property names.
const keys = Object.keys(person); // ["name", "age"]
- Object.values(): Returns an array of a given object's values
const values = Object.values(person); // ["Alice", 30]
Checking if a Key Exists:
- hasOwnProperty(): Checks if the object has the specified property as its own property.
console.log(person.hasOwnProperty("name")); // true
console.log(person.hasOwnProperty("gender")); // false
Connect with me for more articles, tips, and discussions on web development and JavaScript!
#JavaScript #WebDevelopment #Coding #Programming #Tech #LearnToCode #Developer #JavaScriptTutorial