Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why/How should I used objects in JavaScript?

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?

like image 811
David G Avatar asked Aug 03 '11 17:08

David G


People also ask

When should I use objects in JavaScript?

As we know from the chapter Data types, there are eight data types in JavaScript. Seven of them are called “primitive”, because their values contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store keyed collections of various data and more complex entities.

What is the benefit of object in JavaScript?

The advantages of using object literals to create objects include convenience, flexibility in declaration, and less code during declaration. You can drop an object literal anywhere in your program with no previous setup and it'll work, which can be very handy!

How does objects work in JavaScript?

JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method.

How do you use objects?

You also use an object reference to invoke an object's method. You append the method's simple name to the object reference, with an intervening dot operator (.). Also, you provide, within enclosing parentheses, any arguments to the method. If the method does not require any arguments, use empty parentheses.


2 Answers

In your simple example, it makes indeed no sense to write a semi "class" / object to hold that method. But when your code grows, you're getting more and more functions and methods, you don't really want to have them all in one big (global) namespace. That is just so impossible to maintenain, no one will understand that code including you at some later point.

That is the first good reason to wrap methods together in an object/"class". Another good reason is re-usabilty. If you're writting objects which are able to inherit their methods, you can re-create another object and abstract it from there on. Most simple concept, but you want to use it if you're describing "things" in your application as module/object.

like image 52
jAndy Avatar answered Oct 03 '22 02:10

jAndy


Objects are useful for example for making a single unit out of values that belong together. Example:

function Person(firstName, lastName, gender, age) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.gender = gender;
  this.age = age;
}

Person.prototype = {
  getFullName: function() { return this.firstName + ' ' + this.lastName; },
  isMale: function() { return this.gender == 'Male'; },
  isFemale: function() { return this.gender == 'Female'; }
};

var amanda = new Person('Amanda', 'Smith', "Female", 42);
var john = new Person('John', 'Doe', 'Male', 72);

alert(amanda.getFullName());
alert(john.isMale());

Compared to the less structured:

function getFullName(firstName, lastName) {
  return firstName + ' ' + lastName;
}

function isMale(gender) {
  return gender == 'Male';
}

function isFemale(gender) {
  return gender == 'Female';
}

var amandaFirstName = 'Amanda';
var amandaLastName = 'Smith';
var amandaGender = 'Female';
var amandaAge = 42;
var johnFirstName = 'John';
var johnLastName = 'Doe';
var johnGender = 'Male';
var johnAge = 72;

alert(getFullName(amandaFirstName, amandaLastName));
alert(isMale(johnGender));
like image 39
Guffa Avatar answered Oct 03 '22 00:10

Guffa