This is what Kyle Simpson suggests you do instead of faking JavaScript classes in his book this & OBJECT PROTOTYPES and you'll notice he keeps the Pascal case convention for "classes" in his better way of doing things:
var Animal = {
setSpeak: function(noise) { this.vocal = noise },
outputSpeak: function() {
if (this.vocal) {
alert (this.vocal);
} else {
alert ("Hiss!");
}
}
};
var Bat = Object.create(Animal);
Bat.setSpeak("ping");
var Rat = Object.create(Animal);
Rat.setSpeak("squeak");
var Cat = Object.create(Animal);
Cat.actConflictedAndCrazy = function() {
this.outputSpeak();
alert("purr");
}
var myBat = Object.create(Bat);
myBat.outputSpeak();
var myRat = Object.create(Rat);
myRat.outputSpeak();
var myCat = Object.create(Cat);
myCat.actConflictedAndCrazy();
This example spits out four alerts containing...
- ping
- squeak
- Hiss!
- purr
No comments:
Post a Comment