The first couple paragraphs describe what I'm trying to achieve, the actual question is at the end. Thanks
Previously, I've simply been using new keyword to create objects, and prototypes to assign methods and handle inheritance. Recently, however, (partially inspired by CoffeeScript-generated JS) I decided to play with an object-creating function that would look something like this:
var Test = function(a) {
function Test(a) {
this.a = a;
}
var numCalls = 0;
Test.prototype.output = function() {
alert('I was initialized with ' + this.a);
numCalls++;
};
Test.prototype.called = function() {
alert('You called the output ' + numCalls + ' times');
};
return new Test(a);
};
I would then create a new object like this:
var obj = Test('string');
This approach has a couple of advantages over the typical approach that uses new for every instance. First, I'm much less likely to forget using the word new (I know there are other ways of avoiding new, but from what I've seen they have similar problems to the one I describe below), and second, I can easily see 'private' variables that constructor sees in any function that's now part of the class. I did run into a caveat when testing it though. instanceof no longer works because it doesn't see the inner-scoped object Test. To work around that, I tried to use constructor property instead:
var a = Test('one');
var b = Test('two');
a.constructor == b.constructor // false
a.constructor.name == b.constructor.name // true
And this is what got me confused. Creating the same objects without using an object-creating function would cause their constructor to be equal, but in this case they differ. My guess is that what's happening is that a brand new object type gets generated every time the function runs (the object structure is the same, but the instance of the prototype is different).
If my understanding of the problem is correct, does that also mean that the code is allocating additional memory per object instance to my JavaScript for functions that should be shared between instances by tricking it to create an identical object prototype for each instance (defeating the entire purpose of using prototype)? If so, is there a good way to avoid this while still keeping the benefits of this pattern (ability to share private variables between internal functions and not having to use new keyword)?
If I'm misunderstanding the problem, can someone enlighten me on what's actually happening?
newkeyword is not the only advantage to this pattern. Also, I'm not the only programmer on the team. Assuming that everyone will always remember to use thenewkeyword, while also writing the back-end in Python (which doesn't usenewfor classes) is unrealistic. It's much easier to spot a missingnewat the end of class declaration than at every instance creation.