Welcome back, JavaScript enthusiasts, to the JavaScript Learning series! In our previous discussion, we dove into the world of Loops in JavaScript, unraveling their power and versatility. Today, let’s continue our journey by exploring a set of essential JavaScript Object Methods that will enrich your understanding and elevate your coding skills.
1. Object.create():
The Object.create()
method is a powerful tool for creating new objects with a specified prototype. This allows you to inherit properties and methods from an existing object.
Syntax:
javascriptCopy codeObject.create(prototype, [propertiesObject]);
Example:
// Creating a person object as a prototype
let person = {
greet: function() {
console.log(`Hello, ${this.name}!`);
}
};
// Creating a new object using person as the prototype
let pamfa = Object.create(person);
pamfa.name = "Pamfa Ghimire";
pamfa.greet(); // Output: Hello, Pamfa Ghimire!
2. Object.keys():
The Object.keys()
method returns an array of a given object’s property names.
Syntax:
Object.keys(object);
Example:
let scooty = {
brand: "Honda",
model: "Duo",
year: 2018
};
let keys = Object.keys(scooty);
console.log(keys); // Output: ['brand', 'model', 'year']
3. Object.values():
Object.values()
returns an array containing the values of the object’s own enumerable properties.
Syntax:
Object.values(object);
Example:
let scooty = {
brand: "Honda",
model: "Duo",
year: 2018
};
let values = Object.values(scooty);
console.log(values); // Output: ['Honda', 'Duo', 2018]
4. Object.entries():
Object.entries()
returns an array of a given object’s own enumerable property [key, value]
pairs.
Syntax:
Object.entries(object);
Example:
let scooty = {
brand: "Honda",
model: "Duo",
year: 2018
};
let entries = Object.entries(scooty);
console.log(entries);
// Output: [['brand', 'Honda'], ['model', 'Duo'], ['year', 2018]]
5. Object.assign():
Object.assign()
is used for copying the values of all enumerable properties from one or more source objects to a target object.
Syntax:
Object.assign(target, ...sources);
Example:
let scooty = {
brand: "Honda",
model: "Duo"
};
let additionalInfo = {
year: 2018,
color: "Black"
};
let completeScooty = Object.assign({}, car, additionalInfo);
console.log(completeScooty);
// Output: { brand: 'Honda', model: 'Duo', year: 2018, color: 'Black' }
6. Object.freeze():
The Object.freeze()
method freezes an object, preventing new properties from being added, and existing properties from being removed or modified.
Syntax:
Object.freeze(object);
Example:
let person = {
name: "Pamfa",
age: 26
};
Object.freeze(person);
person.age = 31; // This assignment has no effect
console.log(person); // Output: { name: 'Pamfa', age: 26 }
7. Object.is():
Object.is()
compares two values for equality. It returns true
if the values are the same, and false
otherwise.
Syntax:
Object.is(value1, value2);
Example:
console.log(Object.is(5, 5)); // Output: true
console.log(Object.is(0, -0)); // Output: false
8. Object.defineProperty():
The Object.defineProperty()
method defines a new property directly on an object, or modifies an existing property on an object, and returns the object.
Syntax:
Object.defineProperty(object, prop, descriptor);
Example:
let person = {};
Object.defineProperty(person, 'name', {
value: 'Pamfa',
writable: false
});
person.name = 'Champa'; // This assignment has no effect
console.log(person.name); // Output: Pamfa
9. hasOwnProperty():
The hasOwnProperty()
method returns a boolean indicating whether an object has the specified property as its own property, as opposed to inheriting it.
Syntax:
object.hasOwnProperty(prop);
Example:
let scooty = {
brand: "Honda",
model: "Duo"
};
console.log(scooty.hasOwnProperty('brand')); // Output: true
console.log(scooty.hasOwnProperty('color')); // Output: false
These JavaScript Object Methods are powerful tools that can significantly enhance your coding capabilities. By understanding and mastering these methods, you’ll be better equipped to manipulate and manage objects in your JavaScript applications. Stay tuned for more insightful content on datasagar.com, where we continue to unravel the mysteries of JavaScript in our ongoing learning series. Happy coding!