Why/How should I used objects in JavaScript?

Viewed 10328

I understand how to instantiate objects and call them, but I just cannot find a reason to use them in my script. I could do

var obj = {
    hi: function() {
        return "Hello";
    }
}; 

but why can't I just do it the same way like:

function hi() {
    return "Hello";
}

I've never understood the reasons why I should use prototyping either. Most of the things I do in JavaScript I can do well without objects. But I want to use objects. What are objects for and what are the reasons why I should use them?

7 Answers

Some generic thoughts concerning "Why" part:

Objects exist to communicate. It is a common vocabulary for everyone involved in a project. People are used to operate with things (objects) and their operations (messages or methods), rather than just with disconnected actions (functions).

Related