Object Introduction

Objects are JavaScript's fundamental data structure. An object is just a container for a collection of named values (aka properties).

Objects in JavaScript

Objects in JavaScript fall into four groups:

  • User-defined objects - These are custom objects created by the user.
  • Built-in objects - These are provided by the JavaScript language. These include objects associated with data types (String, Number, and Boolean), objects that allow creation of user-defined objects and composite types (Object and Array), and useful in-built objects such as Date, Math, and RegExp.
  • Browser objects - These objects not specified as part of the JavaScript language but that most browsers commonly support. Such browser objects include Window, the object that enables the manipulation of browser windows and interaction with the user, and Navigator, the object that provides information about client configuration.
How to create and use objects
  • What are the constructor functions
  • What types of built-in JavaScript objects exist and what they can do for you

An object is very similar to an array but with the difference that you define the keys yourself. It is not limited to using only numeric indexes but can use keys as your want, such as first_name, age, and so on.

Let's take a look at a simple array and object.

Array:
var myarr = ['red', 'blue', 'yellow', 'purple'];

Below is the index and the values as key/value pairs of above array.

Key Value
0 red
1 blue
2 yellow
3 purple
Object:

The below variable hero is an object.

var hero = {
    breed: 'Turtle',
    occupation: 'Ninja'
};

To define object use { and } and separate the elements (called properties) contained in the object with commas

  • The key/value pairs are divided by colons, as key: value
  • The keys (names of the properties) can optionally be placed in quotation marks.

For example these are all the same:

var o = {
    prop: 1
};
var o = {
    "prop": 1
};
var o = {
    'prop': 1
};

The quotes are necessary for the names of the properties for some cases like below.

  • If the property name is one of the reserved words in JavaScript
  • If it contains spaces or special characters (anything other than letters, numbers, and the underscore character)
  • If it starts with a number
var o = {
    something: 1,
    'yes or no': 'yes',
    '!@#$%^&*': true
};

This is a valid object. The quotes are required for the second and the third properties, otherwise you will get an error.

Elements, Properties, Methods

An object contain properties. In arrays, we say that they contain elements(no difference between the terminology elements and properties).

A property of an object can contain a function, because functions are just data.

In the below example 'talk' property is a method.

var dog = {
    name: 'Benji',
    talk: function() {
        alert('Woof, woof!');
    }
};
Points to remember

It's also possible to store functions as array elements and invoke them.

var a = [];
a[0] = function(what){alert(what);};
a[0]('Boo!');
Hashes, Associative Arrays

In some programming languages, a normal array, also called indexed or enumerated (the keys are numbers) and an associative array, also called a hash (the keys are strings)

JavaScript uses arrays to represent indexed arrays and objects to represent associative arrays. If you want a hash in JavaScript, you use an object.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents