If you have an instance of an object in javascript, it seems it that can be difficult to find its actual type, ie
var Point2D = function Point2D(x, y) {
return {
X: x,
Y: y
}
}
var p = new Point2D(1,1);
typeof p // yields just 'Object' not 'Point2D'
One way around it I found was to make the object its own prototype, and then you can gets its name effectively by calling prototype.constructor.name,
var Point2D = function Point2D(x, y) {
return {
X: x,
Y: y,
prototype: this
}
}
new Point2D(1,1).prototype.constructor.name // yields 'Point2D'
Would this be an OK way of doing it (what are the pros/cons?) or is there a better practice I am missing out on?
Thanks.
Function.prototype.nameis not standard, so it won't be implemented properly or not at all in some browsers.