Hacker News new | past | comments | ask | show | jobs | submit login

"Constructor notation is preferred when you need to set initial properties and methods on an object or you plan on creating multiple instances of an object where the properties and methods of each instance need to be unique."

Love it when they don't just tell you how to do something. Would like to see more people write on when you should use something.




> Love it when they don't just tell you how to do something.

Or why. None of the examples seem like constructors provide any advantage over literals.


So that the methods don't need to be re-defined for every object.


Object literals don't require you to duplicate any code:

    function getObject(val1, val2) {
        return {
            foo: val1,
            bar: function() {
                return val2;
            }
        };
    }


In this instance the "bar" method is redefined every time you call getObject.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: