MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/javascript/comments/192tn4/common_javascript_gotchas/c8kgbds/?context=3
r/javascript • u/stevekwan • Feb 23 '13
50 comments sorted by
View all comments
3
Quick question: which is the 'proper' way to instantiate an object?
var obj1 = {};
or
var obj2 = new Object();
Both return [object Object] and typeof "object". Is one better than the other?
[object Object]
6 u/radhruin Feb 23 '13 There is no semantic difference between {} and new Object(). The literal, however, is easier to optimize and therefore often faster. The literal is also more flexible as it allows additional properties to be added. Always use the literal IMO. 5 u/YorickA Feb 23 '13 Not to mention it's more readable. 1 u/[deleted] Feb 23 '13 Not to mention new Object() can be broken by any malicious ding-dong's script you link like so:function Object() { this.x = 'wee'; }Using the literal is always the best call.
6
There is no semantic difference between {} and new Object(). The literal, however, is easier to optimize and therefore often faster. The literal is also more flexible as it allows additional properties to be added. Always use the literal IMO.
5 u/YorickA Feb 23 '13 Not to mention it's more readable. 1 u/[deleted] Feb 23 '13 Not to mention new Object() can be broken by any malicious ding-dong's script you link like so:function Object() { this.x = 'wee'; }Using the literal is always the best call.
5
Not to mention it's more readable.
1 u/[deleted] Feb 23 '13 Not to mention new Object() can be broken by any malicious ding-dong's script you link like so:function Object() { this.x = 'wee'; }Using the literal is always the best call.
1
Not to mention new Object() can be broken by any malicious ding-dong's script you link like so:
new Object()
function Object() { this.x = 'wee'; }
Using the literal is always the best call.
3
u/wkdown Feb 23 '13
Quick question: which is the 'proper' way to instantiate an object?
var obj1 = {};or
var obj2 = new Object();Both return
[object Object]and typeof "object". Is one better than the other?