My JavaScript constructor looks like this:
var BaseThing = function() {
this.id = generateGuid();
}
When a new BaseThing is created, the ID is unique each time.
var thingOne = new BaseThing();
var thingTwo = new BaseThing();
console.log(thingOne.id === thingTwo.id); // false
However, things become complicated when I attempt 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 behavior is expected due to prototypical inheritance, but it's not what I desire; I want the ID to be unique without needing to re-implement it in each inheriting object.
What would be the most effective approach to achieve this? My own ideas were either (a) reimplementing the id in every child constructor (which seems counterintuitive to inheritance) or (b) incorporating an initialize function into BaseThing (but I don't want to worry about ensuring it's called every time a Thing is created).