Literal notation VS. constructor to create objects in JavaScript [duplicate]

Object literals are usually the way to go. They only need to be parsed when loading the script, which can introduce various optimizations by the scripting engine.

Constructors need to be executed. That means they will be slower, but you can easily add some validation code etc. to them, and they allow the construction of complex objects with public, privileged methods and private “attributes” hidden in the constructors scope. Also, they of course construct objects that share a prototype, which you might find useful.

Leave a Comment