I do not understand quite completely how to apply constructors on this object creation method:
var MyObject = {
...
};
I know that you can do:
var MyObject = new Object();
MyObject.prototype.constructor = function(props)
{
...
}
or…
function MyObject(prop1, prop2)
{
this.prop1 = prop1;
...
}
Can I do something like this?
var MyObject = {
MyObject: function(prop1, prop2)
{
...
}
}
No, you can’t, that would simply create a (static) method on MyObject —
MyObject.MyObject. In JavaScript, a constructor is the class. Class methods and properties are created either inside the constructor usingthis.or by adding to the prototype (outside of the constructor) usingMyClass.prototype.. You can think of “objects” in JavaScript as static classes.