Wednesday, May 21, 2014

N ways to create javascript object

1. Using functions
The method could be added externally by prototype.
It does not matter the code to add getInfo method to prototype happens before or after the new instance happening. Even it happens after the instance is already created, it will still add the new method to the existing instance.

2. Using object literals
Using object literals can create object (instance) immediately.
If I want another instance of it, what shall I do?
Damn it, different ways to do the same work, that is why I hate it... :)
I used to make mistake like this. It does not give you syntax error, but it does not work. The object is empty.
The property and the method could be added externally.

No comments:

Post a Comment