Objects
Prev Chapter 7. JavaScript Basics Next

Objects

Objects contain one or more key-value pairs. The key portion can be any string; if the string contains spaces or is a reserved word, then it must be quoted. The value portion can be any type of value: a number, a string, an array, a function, or even another object.

[Definition: When one of these values is a function, it’s called a method of the object.] Otherwise, they are called properties.

As it turns out, nearly everything in JavaScript is an object — arrays, functions, numbers, even strings — and they all have properties and methods.

Example 7.22. Creating an "object literal"

var myObject = {
    sayHello : function() {
        console.log('hello');
    },

    myName : 'Rebecca'
};

myObject.sayHello();            // logs 'hello'
console.log(myObject.myName);   // logs 'Rebecca'


Prev Up Next
Arrays Home Functions