Hi. I’ve got a simple problem I’m trying to solve, but I can’t find a solution that works online. Here’s what I’m trying to do, boiled down:
1.
Define a class of objects, called Mover, that would basically look like this:
Mover
– has x property– has y property– has move method– has Constructor that takes two arguments, x and y, and sets these to the x and y properties of the object.
Define a second class of objects, let’s say “Cat”, that inherits from Mover, like this:
Cat
– Inherits x, y, and move from Mover.– has breed property.– has a Constructor that takes /three/ arguments {x, y, breed} that sets the breed property to the breed argument, and then passes the arguments to the Mover constructor.
Call one function that returns a new Cat object.
Be able to access Cat.move()
Does that makes sense? I’ve seen all sorts of examples online, and thy get very complicated very quickly, and I haven’t seen one that actually works! Here’s what I’ve got so far:
[CODE]function Mover(x, y){
this.x = x;
this.y = y;
// #note_1
}
Mover.prototype.move = function(delta_x, delta_y){
do_stuff();
};
function Cat(x, y, breed){
// #note_2
this.breed = breed;
}
// #note_3
var my_cat = new Cat(1,2, “tabby”);
#note_1: There are several ways I could have given the Mover class a move function. I could have done “this.move = function”, but that would create a new function for each object. I could have created a global function with a name like “mover_move” and then set “this.move = mover_move”, but that would clutter the list of global functions. Setting a function on the prototype seems to be the way I should be doing this.
#note_2: I could call Mover.apply(arguments) here, but that would only take care of the x and y variables. The my_cat variable would not have a move() method.
#note_3: What am I supposed to be doing here? I feel like I should be doing some sort of complicated Cat.prototype = Mover.something.something.prototype.
I’m also seeing a lot about Object.create(type, {many_braces: {value: magic_number}}), but 1) that is messy as all hell, 2) the value of many_braces cannot be changed from magic_number in the future, 3) this still doesn’t solve the issue of having multiple constructors called. Alternately, I may just have no clue how to use this feature.
So here’s my question: what would you write in order to make a Cat object that inherits from Mover?