So, did some search and found some good readings that explains this topic fairly clearly. Lots of related answers from Stack Overflow is also quite helpful:
- What is the 'new' keyword in JavaScript
- Tim Caswell's "Learning JavaScript with Object Graphs" series (perfect for visual person like me, in II, he specifically mentions about how to create objects besides using new: use Object.create and object factory)
Here is a good article that tests your understanding of "new" keyword ;-)
Now, whether or not you should use "new" is another question:
Using "new" to instantiate a class has both pros (great performance) and cons (can cause nightmare if you forget to use new). Many people including Douglas Crockford discourages the use of new. A module pattern is a more preferable way. John Resig also has a simple solution to avoid using new directly without much performance sacrifice.
No comments:
Post a Comment