JavaScript Object Definition

In JavaScript, objects are the king. If you understand objects, you understand JavaScript.

Almost everything in JavaScript is an object.

  • Booleans are objects (if used new (keyword definition)
  • Numbers are objects (if used new (keyword definition)
  • Strings are objects (if used new (keyword definition)
  • Dates are always objects
  • Arithmetic is always objects
  • Regular expressions are always objects
  • Arrays are always objects
  • Functions are always objects
  • Objects are always objects

All JavaScript values, except for primitive values, are objects.

JavaScript primitive values

Primitive valuesrefers to values that do not have properties or methods.

Primitive data typesrefers to data that has primitive values.

JavaScript defines 5 primitive data types:

  • string
  • number
  • boolean
  • null
  • undefined

Primitive values are immutable (they are hard-coded and therefore cannot be changed).

Suppose x = 3.14, you can change the value of x. But you cannot change the value of 3.14.

value Type Comments
"Hello" string "Hello" is always "Hello"
3.14 number 3.14 is always 3.14
true boolean true is always true
false boolean false is always false
null null (object) null is always null
undefined undefined undefined is always undefined

Objects are variables that contain variables

JavaScript variables can contain single values:

Example

var person = "Bill Gates";

Try It Yourself

Objects are also variables. However, objects can contain many values.

Values are arranged according toName : Valuenames and values separated by colons).

Example

var person = {firstName:"Bill", lastName:"Gates", age:62, eyeColor:"blue"};

Try It Yourself

JavaScript objects are written in the form ofNamed valuesa collection.

Object properties

Named values in JavaScript objects are calledproperty.

property value
firstName Bill
lastName Gates
age 62
eyeColor blue

Objects written in name-value pairs are similar to:

  • Associative Array in PHP
  • Dictionary in Python
  • Hash tables in C
  • Hash maps in Java
  • Hashes in Ruby and Perl

object methods

methods are actions that can be executed on an objectaction.

Object properties can be primitive values, other objects, and functions.

object methodsis containingfunction definitionof the object properties.

property value
firstName Bill
lastName Gates
age 62
eyeColor blue
fullName function() {return this.firstName + " " + this.lastName;}

JavaScript objects are containers for named values called properties and methods.

You will learn more about methods in the next chapter.

Create JavaScript objects

With JavaScript, you can define and create your own objects.

There are different ways to create objects:

  • Define and create a single object, using object literals.
  • Define and create a single object, through the keyword new.
  • Then create an object of the constructor type.

In ECMAScript 5, you can also define an object constructor through the function Object.create() Using object literals

To create an object.

This is the simplest way to create an object.

Using object literals, you can define and create objects in a single statement.

Object literals refer to the curly braces {} The names:values pairs (such as age:62).

The following example creates a new JavaScript object with four properties:

Example

var person = {firstName:"Bill", lastName:"Gates", age:62, eyeColor:"blue"};

Try It Yourself

Spaces and line breaks are not important. Object definitions can span multiple lines:

Example

var person = {
    firstName:"Bill",
    lastName:"Gates",
    age:62,
    eyeColor:"blue"
};

Try It Yourself

Using the JavaScript keyword new

The following example also creates a new JavaScript object with four properties:

Example

var person = new Object();
person.firstName = "Bill";
person.lastName = "Gates";
person.age = 50;
person.eyeColor = "blue"; 

Try It Yourself

The results of the two examples above are the same. No need to use new Object().

For simplicity, readability, and execution speed, please use the first creation method (object literal method).

JavaScript objects are mutable

Objects are mutable: they are addressed by reference, not by value.

If person is an object, the following statement will not create a copy of person:

var x = person;  // This will not create a copy of person.

the object x is not a copy of person. Itis person.x and person are the same object.

Any change to x will change person, because x and person are the same object.

Example

var person = {firstName:"Bill", lastName:"Gates", age:62, eyeColor:"blue"}
var x = person;
x.age = 10;           // This will change both x.age and person.age at the same time

Try It Yourself

Note:JavaScript variables are not mutable. Only JavaScript objects are.