about


Scribe:

chrono

blog

Time

science!

book musings

gov

'punk

missle defense

jams

antiques

cultcha blog


Construct:

scripts & programs

on the town

snaps


Self:

creds (PDF)

key

missive


JavaScript Objects

As in other languages, objects in JavaScript allow the programmer to combine data and functions in a single unit, called an object.

An object is a is a collection of properties, otherwise called an associative array.

Objects can be created in one of two ways, in literal notation, or by using a constructor.

The preferred method, literal notation, puts all the properties witin a set of brackets when the object is defined:

var train = {
  name: "Silver Meteor",
  passengers: 76
};
The second approach consists of creating a constructor, which explicitly creates an object. You can then add properties through dot notation.
var train  = new Object();

train.name = "Silver Meteor";
train.passengers = 76;
The object constructor uses one of two formats. Both are functionally the same, but the first, called object notation, is generally preferred.
var myObject = new Object();

myObject.name = "Joab";
myObject.age = 25;
myObject.gender = "M";
or...

var myObject3 = {};

myObject["name"] = "Joab";
myObject["age"] = 25;
myObject["gender"] = "M";

* * *
As you can see, JavaScript objects can hold different data types. It can hold arrays:
var myArray = [2, 3, 4];

var myObject = {
  name: 'Eduardo',
  type: 'Most excellent',
  interests: myArray,
};
It can even hold other objects:
var friends = {
 steve: {name: "Steve Jobs"},
 bill: {name: "Bill Gates"},
};

In addition to data, you can also add methods to an object. A method is a function that can be called through the object. A function performs some operation, such as adding two numbers together. You can pass a function values, i.e. "("Silver Meteor", 76)" or not "()"

* * *
Each property of an object can be called as a variable. Properties can be called in one of two ways, through dot notation--i.e. "frank.age" is the age property of the Frank object--or through bracket notation--i.e. frank["age"].

* * *
Multiple objects can be grouped together in an array.
* * *

Some architectual underpinnings! In JavaScript, almost everything is an object.

The Window is the global object for a browser. All variables are defined as properties of a window, i.e. "var answer = 42" = "window.answer = 42" (window can also be called "self"). "document" is the document object of the current window.

All objects in JavaScript are arrays. A variable, for instance, is an object and comes with a number of properties, which can be accessed, i.e.

var SampleData = new String("This Is The Sample Data");

(...By using the new keyword, you establish the string as a primitive, rather than an object)

alert(SampleData.length)

(...Accesses the length property of the string)

alert(SampleData.toUpperCase());

(...Converts string to upper case)

Other properties can be used to extract a substring, to concatenate multiple strings together, to slice off a piece of the string, to tokenize a string, to can create an HTML tag, among others.

Example

* * *