Prototypes Explained
by Aaronias
HTML
UserDefinedSource
JavaScript
function Source(a,b,c)
{
var thing = a;
this.a = a;
this.b = b;
this.c = c;
this.hey = function(){
console.log("Source hey");
}
}
Source.prototype.getAnswer = function(p){
// alert(thing);
return this.a * p;
}
Source.prototype.getHelp = function(){
return "helped";
}
function UserDefinedSource(a,b,c)
{
// This call give you access to all of sources functions and attributes
Source.call(this,a,b,c);
this.ho = function()
{
console.log("ho");
}
this.hey = function()
{
console.log("UserDefinedSource hey");
}
}
// This call give UserDefinedSource access to Source's prototype methods, and also
// makes him an instance of Source
// without this call UserDefinedSource is NOT an instanceof Source
//UserDefinedSource.prototype = Object.create(Source.prototype);
UserDefinedSource.prototype = Source.prototype;
/*UserDefinedSource.prototype.calculateTotal = function()
{
return this.a*this.b*this.c;
}*/
var uds = new UserDefinedSource(2,3,4);
// Prototype Methods are static
//uds.hey();
//alert(uds.getHelp());
/*alert(uds.calculateTotal());*/
uds.hey();
console.log(uds.getHelp());
console.log(" instance of UserDefinedSource: " +(uds instanceof UserDefinedSource));
console.log("instance of Source: " +(uds instanceof Source));
console.log(Source.prototype);
//UserDefinedSource a = new UserDefinedSource();
console.log(Source.prototype == UserDefinedSource.prototype);