This is how I do it now. It feels like a hazzle…
var broken_posts = new Object();
broken_posts.empty = new Array();
broken_posts.one = new Array();
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.
The answers by Andru and Thilo are both correct, but perhaps some information on why is in order:
Avoid direct calls to the Array constructor: it’s confusing and misleading.
var a = new Array(5);returns[undefined,undefined,undefined,undefined,undefined], whereasvar b = new Array('5');returns['5']. Or evenvar c = new Array(5,5);=>[5,5].Same applies for the direct object constructor. There’s really no reason to create an object calling the basic object constructor. The only times you should use the keyword
newis when creating a date object, or calling a self-made constructor function (and even in that case, there’s no real need for thenewkeyword, there are alternative design patterns). Using the object literal{}is more common, allows for direct assignment of properties (and even methods). Besides, It’s so much easier to create your objects in a JIT sort of way. Not only does it require less lines of code, with correct use of closures, or just for the one call, an object can get GC’ed once you’re finished with it.As opposed to this scenario:
In the last example chances are: you accidentally create global variables, have various objects in memory that are no longer required, and, last but not least: isn’t very in tune with the prototypal nature of JS.