JavaScript The Good Parts: Objects

Upon a homely object Love can wink
- William Shakespear.

Values leaving numbers, strings, booleans, null and undefined are objects. Numbers, string and boolean are object-like in that they have methods, but they are immuatble. Objects in javascript are mutable keyed collections. In javascript array, function, regular expressions are objects and, of course, objects are objects.

Object is a container of property, where a property has a name and a value. Property names in object can be any string, including empty string. A property value can be any javascript value except for undefined.

Objects in javascript are class-free. There is no constraint on the names of new properties or on values of properties. Objects are useful for collecting and organizing data.

Objects can contain other objects, so they can represent tree or graph structures.

Javascript provides prototype linkage feature that allows one object to inherit the properties of other object. When used well, this can reduce object initialization time and memory consumption.

Object Literal

An object literal is a pair of curly braces surrounding zero or more name/value. Note a property name can be any string including the empty string.

var empty_object = {};
var student = {
"first_name":"Rahul",
"last_name":"Singh"
};
var company = {
name:"Microsoft",
software:"Windows"
};

Retrieval

Undefined is produced if an attempt is made to retrieve value from nonexistent member. TypeError exception is thrown is we try to retrieve value from undefined.

student.place //undefined
student.place.zipcode // TyprError
student.place && student.place.zipcode //undefined

Update

Updates the property’s value if present, else object is augmented.

var student = { name:”Rahul”};
student.name = “Rahul Singh”;
student.college = “MMMEC”;
// If property not present then the object will be augmented.

Reference

Javascript objects are never copied, they are always passed by references.

var a = {},b={},c={};
// all a,b,c point to different object.
var a = b = c ={};
// all a,b,c point to same object.

Prototype

Every object is linked to a prototype object from which it can inherit properties of other objects. All object are linked to Object.prototype, an object that comes standard with JavaScript. So, with it’s help one object can inherit the properties of other objects. Below is an simple method created in the main Object which would make inheriting properties between objects simple. Javascript searches a property throughout the prototypal chain if it’s not present as object’s own property. Has one can use hasOwnProperty() to check if a property is it’s own or not.

if(typeof Object.beget !== ‘function’){
Object.beget = function (o){
var F = function(){};
F.prototype = o;
return new F();
}
}
var new_object = Object.beget(old_object);

Constructors, which are functions whose names usually start with a capital letter, can be used with the new operator to create new objects.The new object’s prototype will be the object found in the prototype property of the constructor function.

Reflection

One can determine properties of an object by retrieving the properties and examining them. One can use following methods to examine:

typeof flight.number //’number’
typeof flight.status //’string’
hasOwnProperty returns true if the object has a particular property. It does not look at the prototype chain.
flight.hasOwnProperty(‘number’) //true
flight.hasOwnProperty(‘constructor’) //false

Enumeration

It is used to loop over all of the property names in an object.

for (name in student){
if(student[name] !== ‘function’ && student[name].hasOwnProperty()){
console.log(name + ‘:’+student[name]);
}
}
// Note in for in loop order is not defined.

Delete

Used to delete a property from an object. But it will not touch any of the property present in the prototype linkage.

student.name //Rahul

delete student.name;
student.name //Hari , form the prototype chain if present.

Global Abatement

One should try to minimize the global variable declaration. By putting all the functions and variables under one global object.


P.S: Play with it to explore more. — nivesh