JavaScript objects are powerful and flexible, and once you're comfortable with the basics, it's time to explore some advanced object features that can make your code cleaner, more dynamic, and easier to maintain. In this article, we'll explore advanced concepts like:
- Object Destructuring
- Computed Property Names
- Object Property Shorthand
- Object Methods
- The
this
keyword - Prototypes & Inheritance
- Object.defineProperty
- Object.freeze & Object.seal
Letβs dive into each of them with practical examples.
π§© 1. Object Destructuring
Destructuring lets you extract values from an object in a clean way.
const user = { name: "Alice", age: 25, country: "USA" };
const { name, age } = user;
console.log(name); // Alice
console.log(age); // 25
You can also rename variables:
const { country: location } = user;
console.log(location); // USA
π 2. Computed Property Names
Dynamic keys can be created using square brackets:
const key = "score";
const obj = {
[key]: 42
};
console.log(obj.score); // 42
Useful when building objects from variables.
βοΈ 3. Property Shorthand
When the variable name and key are the same, you can write:
const name = "John";
const age = 30;
const person = { name, age };
console.log(person); // { name: 'John', age: 30 }
π οΈ 4. Object Methods
You can define functions inside objects:
const calculator = {
add(a, b) {
return a + b;
},
};
console.log(calculator.add(2, 3)); // 5
Note the method shorthand: no need for function
keyword.
π 5. The this
Keyword
Inside object methods, this
refers to the object itself:
const car = {
brand: "Toyota",
getBrand() {
return this.brand;
},
};
console.log(car.getBrand()); // Toyota
β οΈ Be careful: arrow functions donβt have their own this
.
𧬠6. Prototypes & Inheritance
JavaScript objects can inherit properties using prototypes:
const animal = {
speak() {
console.log("Animal speaks");
}
};
const dog = Object.create(animal);
dog.bark = function () {
console.log("Woof!");
};
dog.speak(); // Animal speaks
dog.bark(); // Woof!
ποΈ 7. Object.defineProperty()
Allows fine control over object properties:
const obj = {};
Object.defineProperty(obj, "secret", {
value: "Hidden",
writable: false,
enumerable: false
});
console.log(obj.secret); // Hidden
obj.secret = "Exposed";
console.log(obj.secret); // Still Hidden
Useful for creating read-only or hidden properties.
π§ 8. Object.freeze()
and Object.seal()
-
Object.freeze(obj)
β Makes the object completely immutable. -
Object.seal(obj)
β Allows changes to existing properties but disallows adding or deleting.
const config = {
apiKey: "123"
};
Object.freeze(config);
config.apiKey = "456"; // Wonβt change
console.log(config.apiKey); // 123
π Conclusion
Advanced object features in JavaScript can help you write more concise, flexible, and scalable code. Whether youβre working on a small script or a large application, mastering these features can give you a major edge.
Keep experimenting with objects β theyβre the backbone of JavaScript programming!
Top comments (6)
wow, gotta admit using stuff like destructuring and property shorthand sorta made life so much easier for me - you think theres ever a point where knowing more features actually slows you down instead of helping?
Thanks so much for your comment! You bring up a great point. I totally get how advanced features like destructuring or property shorthand can feel like a double-edged sword. Personally sometimes hurt readability, especially for someone newer to JavaScript.
nice article bro π₯
Thanks a ton, bro!
Glad you liked itβmeans a lot
Very experienced bro!
Thanks a lot, Still learning every day β really appreciate your kind words!