Home
XHTML
CSS
PHP
MySQL
SEO
JavaScript
Computer Basics
Number Systems
LINUX


JavaScript Objects

JavaScript | Programming | Decisions | Objects

Javascript is an object-orientated programming language. Everything in JavaScript is treated as an object. An object, like an array, is basically any collection of names and values, also known as properties and methods. Properties are variables local to the object. Methods are functions local to the object. An object serves as a template, which instances can be called for use of its properties or methods. Objects are built-in or they can be created.

A common built-in object is the  document  object, which refers to the JavaScript file itself. Properties and/or methods are added with dot syntax. The following example appends the  write()  method to the  document  object while passing a variable...

document.write(variableName);


Creating an object is similar to creating a function. The function command declares a new object, followed by the name of the object. Objects are created with the same naming conventions as a variable. The object definition is declared within  { }  side brackets. Properties are stored in variables. The this command references the variable. The var command declares a variable within the object. The new command calls an object, storing it in a new variable. The new variable becomes the instance of the object.

function objectName() {
this.propertyName="This is a value.";}

var instanceName=new objectName;

document.write(instanceName.propertyName);

The output is...

This is a value.



The following example declares a method and passes parameters to the object...

function ObjectName(name, city) {
this.propertyName1=name;
this.propertyName2=city;

this.methodName = function() {
return "Name: " + this.propertyName1 +
"<br />City: " + this.propertyName2;}}

instanceName=new ObjectName("Jeff", "Vancouver");

var newObject=instanceName.methodName();

document.write(newObject);

The output is...

Name: Jeff
City: Vancouver




SEO Vancouver, Washington
SEO Portland, Oregon
Website Design Vancouver, Washington