Object initialization:
var myObject = {};
and
var myObject = new Object();
why the last one is considered an antipattern?
thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m not sure
new Object()really falls under “anti-pattern”, but{}is usually preferred, it’s terse and more flexible at the same time.When you’re writing JavaScript you want it to end up short…if it can be terse and understandable when writing it as well, as this is:
…then you get the best of both worlds. Also when you’re creating objects within objects, for example:
…then you can see it’s much more readable than the alternative
new Object()syntax.