I've got a problem. I'm working on a fairly large JavaScript project, and I'm learning how to use OOP. It's kind of weird in JS, as I'm sure you know.
So here's what I'm trying to do: I've got a base 'class', that I call AbstractAnimal
. I've got another class called Animals
. Inside of the Animals
class I have three groups, called Man
, Monkey
, and Elephant
. I want each of those groups to inherit AbstractAnimal
, but to be their own functions, not connected to AbstractAnimal
.
I was originally doing that like this:
Animals.prototype.Man = AbstractAnimal;
Animals.prototype.Monkey = AbstractAnimal; //etc...
However, that doesn't do what I need it to do, sadly. Here's why:
AbstractAnimal.prototype.clicked = function() { console.log("clicked") };
//If I change Man...
animals.Man.clicked = function() { console.log("HA!"); };
//Monkey is effected too...
animals.Monkey.clicked();
//Console: "HA!"
In the example above, when Monkey is clicked I would want the console to say "clicked". When Man is clicked, it should say "HA!".
So I found something that I thought would do it over here: javascript class inherit from Function class
And it almost worked, but not quite. Here's a reduced test case: http://jsfiddle.net/9KRJk/2/
Could'ja help me out? Thanks!
P.S. No, actually, the whole animal thing is metaphorical, I'm not actually programming a zoo. :)