HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JavaScript Objects


Object is the main data type in JavaScript. "Everything" in JavaScript is an Object.

In addition, JavaScript allows you to define your own objects.


Everything is an Object

In JavaScript almost everything is an object.

Even primitive data types (except null and undefined) can be treated as objects.

  • Booleans can be objects (or primitive data treated as objects)
  • Numbers can be objects (or primitive data treated as objects)
  • Strings are also objects (or primitive data treated as objects)
  • Dates are always objects
  • Maths and Regular Expressions are always objects
  • Arrays are always objects
  • Even functions are always objects

JavaScript Objects

A JavaScript object is a complex variable, with properties and methods.

The content of an object is composite: It can contain multiple property types (both primitive values and other objects).   

In JavaScript, all values except primitive values are objects.

Primitive values are: strings ("John Doe"), numbers (3.14), true, false, null, and undefined.  


Object Properties

Properties are the values associated with an object.

A JavaScript object is an unordered collection of properties.

Each property has a name and a value.

The syntax for accessing the property of an object is:

objectName.property

or

objectName[expression]

You will learn more about properties in the next chapter.


Object Methods

Methods are the actions that can be performed on objects.

You can call a method with the following syntax:

objectName.methodName()

You will learn more about methods in the next chapter.


Creating a JavaScript Object

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

There are different ways to create new objects:

  • Define and create a single object, using an object literal.
  • Define and create a single object, with the keyword new.
  • Define an object constructor, and then create objects of the constructed type.
Note In ECMAScript 5, an object can also be created with the function Object.create().


Using an Object Literal

This is the easiest way to create a JavaScript Object.

Using an object literal, you both define and create an object in one statement.

An object literal is a list of name:value pairs (like age:50) inside curly braces {}.

The following example creates a new JavaScript object, and adds four properties to it:

Example

var person = {firstname:"John", lastname:"Doe", age:50, eyecolor:"blue"};

Try it yourself »


Using the Keyword: new

The following example also creates a new JavaScript object, and adds four properties to it:

Example

var person = new Object();
person.firstname = "John";
person.lastname = "Doe";
person.age = 50;
person.eyecolor = "blue";

Try it yourself »

The two examples above do exactly the same.


Using an Object Constructor

The examples above are limited in many situations. They only create a single object.

Sometimes we like to have an "object type" that can be used to create many objects of one type.

The standard way to create an "object type" is to use an object constructor function:

Example

function person(firstname, lastname, age, eyecolor)
{
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
this.eyecolor = eyecolor;
}
var myFather = new person("John", "Doe", 50, "blue");
var myMother = new person("Sally", "Rally", 48, "green")

Try it yourself »

The above function (person) is an object constructor.

Once you have an object constructor, you can create new objects of the same type:

var myFather = new person("John", "Doe", 50, "blue");
var myMother = new person("Sally", "Rally", 48, "green");

The reason for the "this" thing is to set the properties of the constructor.


Built-in JavaScript Constructors

JavaScript has built-in constructors for native objects:

Example

var x = new Date();      // A new Date object = current time
var x = new Array();     // A new Array object = []
var x = new String();    // A new String object = "" 
var x = new Number();    // A new Number object = 0
var x = new Boolean()    // A new Boolean object = false
var x = new RegExp();    // A new pattern matching object = /(?:)/
var x = new Function();  // A new Function object = anonymous() {}
var x = new Object();    // A new Object object = {}

Try it yourself »


Constructor Notes

There is no reason to use new Array(). Use array literals instead: []

There is no reason to use new String(), new Number(), or new Boolean: Read next paragraph.

There is no reason to use new RegExp(). Use pattern literals instead: /(?)/

There is no reason to use new Function(). Use function expressions instead: function () {}.

There is no reason to use new Object(). Use object literals instead: {}

The Math() object is not in the list above. Math is a global object. The keyword new cannot be used on global objects.


String, Number, and Boolean Objects

As you can see, JavaScript has object versions of the primitive types String, Number, and Boolean.

Written as objects:

var x = new String();
var y = new Number();
var z = new Boolean();

Written as primitives:

var x = "";
var y = 0;
var z = false;

There is no reason create complex objects. Primitive values execute much faster.


JavaScript Objects are Mutable

Object are mutable: They are addressed by reference, not by value.

If y is an object, then:

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

The object x is not a copy of y. It is a new reference (pointer) to y.

Any changes to y will also change x, because x and y is the same object.

Example

var person = {firstname:"John", lastname:"Doe", age:50, eyecolor:"blue"}
var x = person
x.age = 10;  // This will change both x.age and person.age

Try it yourself »


JavaScript Prototypes

All JavaScript objects have a prototype object.

All JavaScript objects inherits the properties and methods from its prototype.

Objects created using an object literal, or with new Object(), inherits from a prototype called Object.prototype.

Objects created with new Date() inherits the Date.prototype.

The Object.prototype is on the top of the prototype chain.

All JavaScript objects (Date, Array, RegExp, Function, ....) inherits from the Object.prototype.



Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE