JavaScript Objects Part 1


The book JavaScript: The Good Parts by Douglas Crockford, Copyright 2008 Yahoo Inc. published by O’Reilly Media Inc. explains objects this way:

The simple types of JavaScript are numbers, strings, booleans (true and false), null, and undefined. All other values are objects. Numbers, strings, and booleans are object-like in that they have methods, but they are immutable. Objects in JavaScript are mutable keyed collections. In JavaScript, arrays are objects, functions are objects, regular expressions are objects, and, of course, objects are objects.

JavaScript is class-less. You do not create classes, as you would in C#. In C# classes you need to create a class and then instantiate that class with the new keyword. This instantiation allocates memory for the object and creates it. With JavaScript, you just go ahead and start creating your objects.

An object is a container of properties, where a property has a name and a value. A property name can be any string, including the empty string. A property value can be any JavaScript value except for undefined.

var emptyobject = {};
var person = {
    firstname: "Billy",
    lastname: "Bob",
    gender: "male",
    "lifetime member": true,
    amount: 45.34
};
emptyobject.age = 25;

For the property “lifetime member” you need to use quotes because it is two words with a space in the middle. Notice that the emptyobject is no longer empty. Objects are mutable. The expression firstname: “Billy” is a name/value pair. An object literal is a pair of curly braces surrounding zero or more name/value pairs. Notice that text is in quotes, numbers don’t use quotes and the boolean true and false do not need quotes. Don’t use a comma after the last property value.

Here is another example. It is from the book JavaScript The Good Parts. An object can contain objects.

var flight = {
	airline: "Oceanic",
	number: 815,
	departure: {
		IATA: "SYD",
		time: "2004-09-22 14:55",
		city: "Sydney"
		},
	arrival: {
		IATA: "LAX",
		time: "2004-09-23 10:42",
		city: "Los Angeles"
}

A value in an object can be updated by assignment. If the object does not already have that property name, the object is augmented. You can extend your object at any time with new properties. As long as the property doesn’t already exist in the object, it’s added to the object. Otherwise, the property with this name is updated.

var person = {
    firstname: "Billy",
    lastname: "Bob",
    gender: "male",
    "lifetime member": true,
    amount: 45.34
};
var x = person;  
// x is now a reference (or pointer) to the person object.
// x and person are references to the same object.
x.lastname = "Thorton";
var hisname = person.lastname;
// hisname is now Thorton
var a = {}, b = {}, c = {};
// a, b, and c each refer to a
// different empty object
a = b = c = {};
// a, b, and c all refer to
// the same empty object

You can use an object’s property just like you use a variable, except you need to use dot notation to access the property in the object.

As you know, to add a property to an object you simply assign a value to a new property, like this:

fido.dogYears = 35;

From that point on fido will have a new property dogYears. Easy enough. To delete a property, we use a special keyword, delete. You use the delete keyword like this:

delete fido.dogyears;

When you delete a property, you’re not just deleting the value of the property, you’re deleting the property itself. And, if you try to use fido.dogYears after deleting it, it will evaluate to undefined.

Leave a comment

Your email address will not be published. Required fields are marked *