Fandom

JavaScript Wiki

Tutorial/Objects

< Tutorial

87pages on
this wiki
Add New Page
Talk0 Share
A page in the
 
Introduction
 
Basics
Statements · Control Flow Statements · Comments · Objects · Functions · Style
Features
Scoping · Inheritance · DOM


It has been suggested that this page or section be merged into Wikibooks:JavaScript. (Discuss)

An object is a great way to keep track of a bunch of related information. Additionally, the information contained in one object can be "inherited" by another. For example, imagine a program which represents cars. Other languages like PHP, Java, or C++ force the programmer to act as a manufacturer, providing a blueprint with every detail of how each vehicle's parts will work before it ever runs. But JavaScript lets you add and remove parts as needed, so you can have a Ferrari one second and a seaplane the next. And if at some point you particularly like the current arrangement, you can instantly make unlimited copies.

SyntaxEdit

Main article: Object#Literal_syntax

Objects are initialized via braces {}; they may be populated either during initialization or later. The following examples are effectively identical:

var obj = {foo: 'bar'};


var obj = {};
obj.foo = 'bar';

If a property is accessed before being initialized, its value is undefined.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.