Skip to content

Instantly share code, notes, and snippets.

@ansarisufiyan777
Created September 5, 2019 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ansarisufiyan777/dce2778a1c31ff164097a01f9a55dc5d to your computer and use it in GitHub Desktop.
Save ansarisufiyan777/dce2778a1c31ff164097a01f9a55dc5d to your computer and use it in GitHub Desktop.
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) {
Parent.call(this, "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) {
Child.call(this, "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
*/
ocp.PrintParent();
var ocp = new Parent()
/**
* new keyword always call a constructor of the function hence we are getting an object with value unknown
*/
ocp.PrintParent();
function Person (name, city) {
this.name = 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);
console.log("willem.prototype:",willem.prototype);
console.log("Person.prototype:",Person.prototype);
console.log("willem.__proto__",willem.__proto__);
console.log("willem.age",willem.age);
console.log("Person.prototype.age",Person.prototype.age);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment