Skip to content

Instantly share code, notes, and snippets.

@k33g
Created January 13, 2011 19:42
Show Gist options
  • Save k33g/778460 to your computer and use it in GitHub Desktop.
Save k33g/778460 to your computer and use it in GitHub Desktop.
java like inheritance and shameful use of prototype
function Human(){
var that = this;
var name="John Doe";
that.getName=function(){return name;}
that.setName=function(value){name=value;}
return that;
}
function Man(){
var that = this.prototype = new Human();
this.prototype.constructor = Man;
//this.prototype.constructor = this;
var nickName = "Johnny";
that.getNickName=function(){return nickName;}
that.setNickName=function(value){nickName =value;}
that.toString = function(){return "Hello i am "+that.getName()+" aka "+nickName;}
return that;
}
var bob = new Man(); bob.setName("Bob"); bob.setNickName("Bobby");
var bill = new Man(); bill.setName("Bill"); bill.setNickName("Billy");
console.log(bob.toString());
console.log(bill.toString());
Human.prototype.helloWorld = function(){return "Hello World by "+ this.getName();}
console.log(bob.helloWorld());
console.log(bill.helloWorld());
console.log(bob);
console.log(bill);
function SuperHeroe(){
var that = this.prototype = new Man();
this.prototype.constructor = SuperHeroe;
//this.prototype.constructor = this;
var power = "?";
that.getPower = function(){return power};
that.setPower = function(value){power = value;}
return that;
}
var clarkkent = new SuperHeroe();
clarkkent.setName("Clark Kent");
clarkkent.setNickName("Smallville");
clarkkent.setPower("flying");
var peterparker = new SuperHeroe();
peterparker.setName("Peter Parker");
peterparker.setNickName("Pete");
peterparker.setPower("climbing");
console.log(clarkkent.toString()+" "+clarkkent.getPower());
console.log(peterparker.toString()+" "+peterparker.getPower());
console.log(clarkkent.helloWorld());
console.log(peterparker.helloWorld());
console.log(clarkkent);
console.log(peterparker);
Human.prototype.ditBonjour = function(){return "Salut à tous par "+ this.getName();}
console.log(clarkkent.ditBonjour());
console.log(peterparker.ditBonjour());
console.log(bob.ditBonjour());
console.log(bill.ditBonjour());
@k33g
Copy link
Author

k33g commented Jan 17, 2011

Je vais donc changer le titre par "java like inheritance and shameful use of prototype"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment