In each case below, the details were obtained via a screenshot of
console.dir(myObject)
in the Google Chrome browser.
Method | Code & Initial Details |
---|---|
Object Literal |
|
Create Object Literal via a Function |
|
Create Object Literal via a Function that has Local Variable |
|
Use Function as Constructor |
|
The most obvious difference here is what happens when the
new
operator is used with the function. However, while the screenshot of the others is identical, there are differences based on the associated closures with each function. There is a way to dive into this with Chrome's Heap Profiler, but I haven't yet figured out an easy way to get that out.
Using the
new
operator lets you easily create lots of different objects of the same type - you can add constructor arguments to facilitate customizing each one.
More Reading
- Douglas Crockford's thoughts on newfrom 2006
- This discussion on a stackoverflow post about new
- Information on working with objects in javascript on the Mozilla Developer Network site
- Great explanation on closures in javascript at javascript.info
No comments:
Post a Comment