I have a javascript class that looks like this:
function xyPoint(x, y)
{
this. x = x;
this.y = y;
this.hasX = function()
{
return (this.x != null);
}
this.dumbFunction = function()
{
if (this.hasX())
//do something
}
}
How do I get this this.hasX() to execute? It does not seem to work.
Using that pattern will eat memory like candy, you are creating a new closure every time you instantiate
xyPoint.. Don’t define any functions inside the constructor. Use the prototype object so they are only defined once:Now that the important thing has been said… you would use it like this (both versions):