Search This Blog

2023/04/19

Object.create in javascript



Object.create is a built-in function in JavaScript that creates a new
object with a specified prototype object and properties.
The syntax for Object.create is:

Object.create(proto[, propertiesObject])


The first argument, proto, is the prototype object to be used for the new object.
The second argument, propertiesObject, is an optional parameter that allows you to
additional properties for the new object.

Here's an example of how Object.create can be used:

const animal = {
makeSound: function() {
console.log('The animal makes a sound.');
}
};

const dog = Object.create(animal, {
breed: {
value: 'Labrador',
writable: true,
enumerable: true,
configurable: true
}
});

dog.makeSound(); // Output: "The animal makes a sound."
console.log(dog.breed); // Output: "Labrador"

No comments:

Post a Comment