Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
function Parent(name, address) {
this.fatherName = name || "unknown";
this.fatherAddress = address || "Unknown";
Parent.prototype.PrintParent = function () {
console.log("In Parent",this);
console.log("Creating child")
function Child(name, address) {, "Nafees", "Delhi")
this.childName = name
this.childAddress = address
Child.prototype = new Parent();
//If constructor not set to a Child then Child constructor will automatically be a Parent
Child.prototype.constructor = Child;
Child.prototype.PrintChild = function () {
console.log("In child",this);
console.log("Creating grand child")
function GrandChild(name, address) {, "Sufiyan", "Ghatkopar")
this.grandChildName = name
this.grandChildAddress = address
GrandChild.prototype = new Child();
//If constructor not set to a grandchild then Grandchild constructor will automatically be a Child
GrandChild.prototype.constructor = GrandChild;
GrandChild.prototype.PrintGrandChild = function () {
console.log("In Grand Child",this);
var gc = new GrandChild("A","B");
console.log("Grand child object:",gc)
console.log("Grand child constructor:",gc.constructor)
console.log("gc instanceof GrandChild:",gc instanceof GrandChild)
console.log("gc instanceof Child:",gc instanceof Child)
console.log("gc instanceof Parent:",gc instanceof Parent)
var c = new Child("Sufiyan","Ghatkopar");
console.log("Child constructor:",c.constructor)
console.log("Child object:",c)
console.log("c instanceof Parent:",c instanceof Parent)
console.log("c instanceof GrandChild:",c instanceof GrandChild)
var p = new Parent("Nafees","Delhi");
console.log("Parent object:",p)
console.log("Parent constructor:",p.constructor)
console.log("p instanceof Child:",p instanceof Child)
console.log("p instanceof GrandChild:",p instanceof GrandChild)
//Difference between object.create and new keyword
var ocp = Object.create(Parent.prototype);
* Object.create never calls constructor of the function hence this will print an empty object
var ocp = new Parent()
* new keyword always call a constructor of the function hence we are getting an object with value unknown
function Person (name, city) { = name;
Person.prototype.age = 25;
const willem = new Person('Willem');
// the __proto__ property on the instance refers to the prototype of the constructor
console.log("willem Object",willem);
console.log("Person function",Person);
console.log("willem === Person:",willem === Person);
console.log("willem.__proto__ === Person.prototype:",willem.__proto__ === Person.prototype);
console.log("willem.__proto__ === Person.__proto__:",willem.__proto__ === Person.__proto__);
console.log("willem.__proto__ === willem.prototype:",willem.__proto__ === willem.prototype);
console.log("willem.__proto__ === willem:",willem.__proto__ === willem);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.