Objects
For convenience, Types are normally subdivided into primitives
and objects
. Objects are entities that have an identity (they are only equal to themselves) and that map property names to values, ("slots" in prototype-based programming terminology). JavaScript objects are often mistakenly described as associative arrays or hashes, but they are neither.
JavaScript has several kinds of built in objects, namely Array, Boolean, Date, Function, Math, Number, Object, RegExp and String. Other objects are "host objects", defined not by the language but by the runtime environment. For example, in a browser, typical host objects belong to the DOM (window, form, links etc.).
Creating objects
Objects can be created using a declaration, an initialiser or a constructor function:
// Declaration
var anObject = new Object();
// Initialiser
var objectA = ;
var objectB = {'index1':'value 1','index2':'value 2'};
// Constructor (see below)
Constructors
Constructor functions
are a way to create multiple instances or copies of the same object. JavaScript is a prototype based object-based language. This means that inheritance is between objects, not between classes (JavaScript has no classes). Objects inherit properties from their prototypes.
Properties and methods can be added by the constructor, or they can be added and removed after the object has been created. To do this for all instances created by a single constructor function, the prototype
property of the constructor is used to access the prototype object. Object deletion is not mandatory as the scripting engine will garbage collect any variables that are no longer being referenced.
Example: Manipulating an object
// constructor function
function MyObject(attributeA, attributeB) {
this.attributeA = attributeA;
this.attributeB = attributeB;
}
// create an Object
obj = new MyObject('red', 1000);
// access an attribute of obj
alert(obj.attributeA);
// access an attribute using square bracket notation
alert(obj["attributeA"]);
// add a new property
obj.attributeC = new Date();
// remove a property of obj
delete obj.attributeB;
// remove the whole Object
delete obj;
JavaScript supports inheritance hierarchies through prototyping. For example:
function Base() {
this.Override = function() {
alert("Base::Override()");
}
this.BaseFunction = function() {
alert("Base::BaseFunction()");
}
}
function Derive() {
this.Override = function() {
alert("Derive::Override()");
}
}
Derive.prototype = new Base();
d = new Derive();
d.Override();
d.BaseFunction();
d.__proto__.Override(); // mozilla only
will result in the display:
Derive::Override()
Base::BaseFunction()
Base::Override() // mozilla only
Object hierarchy may also be created without prototyping:
function red() {
this.sayRed = function () {
alert ('red wine')
}
}
function blue() {
this.sayBlue = function () {
alert('blue sky')
}
this.someName = black // inherits black
this.someName() // inherits black
}
function black () {
this.sayBlack = function () {
alert('black night')
}
}
function anyColour() {
this.anotherName = red // inherits red
this.anotherName() // inherits red
this.sayPink = function() {
alert('"Any Colour You Like" is a song of Pink Floyd')
}
this.anotherName = blue // inherits blue ( + black )
this.anotherName() // inherits blue ( + black )
this.anotherName = 'released 1973' // now it's a string - just for fun
}
var hugo = new anyColour()
hugo.sayRed()
hugo.sayBlue()
hugo.sayBlack()
hugo.sayPink()
alert(hugo.anotherName)
|