Javascript doesn't really have classes. What it has is prototypes -- an instance of an object that is used as a template for new objects.
The way you've created your object is to use a literal constructor. It's succinct, but suffers that it cannot be added to or use complicated statements in its construction.
Another way is like so:
function SomeClass(value) {
if (value < 0) {
this.field = -1;
} else {
this.field = value;
}
}
And a new instance is created like this:
var obj = new SomeClass(15);
This enables you to use conditional logic, for loops and other more complicated programming techniques in construction of your object. However, we can only add instance fields and not 'class' fields. You add class fields my modifying the prototype
of your object creator function.
MyClass.prototype.fieldSquared = function () {
return this.field * this.field;
}
This gives a more complete overview of object creation and prototypes in Javascript.