C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Object.create() MethodThe Object.create() method is used to create a new object with the specified prototype object and properties. We can create an object without a prototype by Object.creates (null). Syntax:
Object.create(prototype[, propertiesObject]) Parameter
prototype: It is the prototype object from which a new object has to be created. propertiesObject: It is an optional parameter. It specifies the enumerable properties to be added to the newly created object. Return
Object.create() returns a new object with the specified prototype object and properties. Browser Support:
Example 1
const people = {
printIntroduction: function ()
{
console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
}
};
const me = Object.create(people);
me.name = "Marry"; // "name" is a property set on "me", but not on "person"
me.isHuman = true; // inherited properties can be overwritten
me.printIntroduction();
Output: "My name Marry. Am I human? true" Example 2
function fruits() {
this.name = 'franco';
}
function fun() {
fruits.call(this)
}
fun.prototype = Object.create(fruits.prototype);
const app = new fun();
console.log(app.name);
Output: "franco" Example 3
function fruits() {
this.name = 'fruit';
this.season = 'Winter';
}
function apple() {
fruits.call(this);
}
apple.prototype = Object.create(fruits.prototype);
const app = new apple();
console.log(app.name,app.season);
console.log(app.season);
Output: "fruit" "Winter" "Winter"
Next TopicJavaScript Objects
|