I have a constructor like this in JavaScript:
var BaseThing = function() {
this.id = generateGuid();
}
As you'd expect, when you create a new BaseThing, the ID is unique each time.
var thingOne = new BaseThing();
var thingTwo = new BaseThing();
console.log(thingOne.id === thingTwo.id); // false
But things get hairy when I try to create objects that inherit from BaseThing:
var FancyThing = function() {
this.fanciness = "considerable";
}
FancyThing.prototype = new BaseThing();
var thingOne = new FancyThing();
var thingTwo = new FancyThing();
console.log(thingOne.id === thingTwo.id); // true
This of course makes sense because of the way prototypical inheritance works, but it's not what I want; I want the ID to be unique without having to reimplement it on each object that inherits from BaseThing.
What's the best way to do this? The only solutions I could come up with myself were to (a) reimplement the id on each child constructor (but this seems to defeat the point of inheritance) or (b) add some sort of initialize function to BaseThing (but I don't want to have to worry about making sure it's called every time a Thing is created).