More Prototyping
by Aaronias
JavaScript
console.clear();
function Parent(fromChild)
{
if(angular.isUndefined(this.id))
{
this.id = Math.random();
}
if(!fromChild)
{
console.log("Parent id:" + this.id);
}
else
{
console.log("Child id:" + this.id);
}
this.par = function()
{
console.log("Called from parent");
}
}
function Child()
{
Parent.call(this, true);
this.chi = function()
{
console.log("Called from child");
}
}
function Child2()
{
Parent.call(this, true);
this.chi = function()
{
console.log("Called from child");
}
}
function GrandChild()
{
Child.call(this);
this.grand = function()
{
console.log("Called from grandchild");
}
}
//Child.prototype = Object.create(Parent.prototype);
Child.prototype = Parent.prototype;
Child2.prototype = Parent.prototype;
//GrandChild.prototype = Object.create(Child.prototype);
GrandChild.prototype = Child.prototype;
var c = new Child();
c.chi();
c.par();
console.log(c instanceof Parent);
var g = new GrandChild();
g.grand();
g.par();
g.chi();
var p = new Parent();
var c2 = new Child2();
console.log(c2 instanceof Child);
console.log(new Parent() instanceof Child);