I'm doing my first javascript project that makes heavy use of objects. Because of how it works, nearly all the custom objects are done like this:
namespaceobj = {};
namespaceobj.subobject = {};
namespaceobj.subobject.somefunction = function(arg, uments) {
// Do Stuff
}
namespaceobj.subobject.somedata = 10;
namespaceobj.othersubject = {};
namespaceobj.othersubject.somefunction = function(some, args) {
// Do more stuff
}
// More subobjects etc.
Which is fine, as all the custom objects only have a single instance anyway (examples of subobjects are the UI, the tools, the shared data, etc.).
However I have seen code done something like this (syntax is probably wrong, this is just from memory of seeing similar code)
function SomeClass() {
this.somedata = 42;
this.somefunction = function(a, few, args) {
// Do Stuff
}
}
// More classes and stuff
// Elsewhere:
someInstance = new SomeClass(); // AFA I recall, new was optional
someInstance.somefunction();
Could someone explain how the "classes" in the second example work, and any pitfalls I might encounter while using them.