Crockford\'s book, JavaScript: The Good Parts, says (on page 114) that constructor functions should always be given names with an initial capital letter (i
You can avoid "new" by returning an anonymous object and using a closure in your constructor. This also help you hide private data.
Consider:
function SomeCounter(start) {
var counter = start;
return {
getCounter : function() {
return counter;
},
increaseCounter : function() {
counter++;
}
};
}
Now to use this, all you need to do is
var myCounter = SomeCounter(5);
myCounter.increaseCounter();
console.log(myCounter.getCounter()); //should log 6
The beauty of this is that you do not need to remember to use "new", but if you do it won't hurt you.
var myCounter = new SomeCounter(5); //still works