Thursday, June 14, 2018

JavaScript - Object Creation - Techniques - Object.create()

Below code demonstrates usage of Object.create() to create objects:

var age = 30;

var person1 = {
  name : "Diana",
  age : 27,
};

console.log(person1);

// using prototype as null - that means there is no prototype
var person2 = Object.create(null);
person2.name = "Sandra";
person2.age = "28";
console.log(person2);

// using prototype as person1  
//that means inheriting from object - person1
var person3 = Object.create(person1);
person3.name = "Cassandra";
console.log(person3);



Refer:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create

Practice at:
http://jsbin.com

No comments:

Post a Comment