Archive for September, 2007

JavaScript: Enumerating Properties of an Object with a For/In Loop

Posted in JavaScript, Software, programming on September 29, 2007 by Joey

JavaScript object properties can be enumerated by using a for/in loop, as demonstrated below:

var objCar = new Object();
objCar.wheels = 4;
objCar.tires = 4;
objCar.engines = 1;

for (var i in objCar)
{
document.write(i);
document.write('<br/>');
}

The result of this code is:

wheels
tires
engines

How to Create an Object in JavaScript

Posted in JavaScript, Software, programming on September 29, 2007 by Joey

An object can easily be created in JavaScript by creating a new instance of the native JavaScript Object class. Following this, properties can be assigned to it, as follows:

var objCar = new Object();
objCar.wheels = 4;

document.write(objCar.wheels);

JavaScript Instance Methods

Posted in JavaScript, Software, programming with tags on September 29, 2007 by Joey

A constructor that is created in JavaScript can have methods defined for it. These are called ‘instance methods.’

For example, suppose the following constructor was defined in JavaScript:

function Square(intSideLength)
{
this.sideLength = intSideLength;
}

In this example, ’sideLength’ is an instance property and each instantiation of the Square class will have it’s own copy of the ’sideLength’ property. Instance methods work a little differently. They are defined through the prototype property of the constructor. For example:

Square.prototype.perimeter = function() {return 4*this.sideLength;}
document.write(mySquare.perimeter());

The perimeter method is thus an instance method of the Square class. Instance methods differ from instance properties in JavaScript in that each instantiation of the instance method is shared among instantiations of the class.

Related articles:
Object-Oriented JavaScript
JavaScript Instance Properties

How to Enable and Disable JavaScript in Internet Explorer

Posted in JavaScript, Technology with tags , , on September 29, 2007 by Joey

JavaScript can easily be enabled and disabled in Internet Explorer through a check box that is found at the following location in Internet Explorer:

  • Click ‘Tools’ on the menu bar’
  • Select ‘Internet Options’
  • Click the ‘Security’ tab
  • Click the ‘Custom Level’ button
  • Scroll to the ‘Scripting’ section
    • Select ‘Disable’ to disable JavaScript
    • Select ‘Enable’ to enable JavaScript
    • Select ‘Prompt’ to be prompted whether to allow JavaScript to run on a page

Related Articles:
How to Enable and Disable JavaScript in Mozilla Firefox

What is a Mashup?

Posted in JavaScript, Software, Technology, programming with tags , on September 29, 2007 by Joey

A mashup is a web application hybrid. Mashups take information from more than one source and combine it into one application. The combination of these multiple applications makes the mashup more useful collectively than the individual applications are themselves.

A common example of a mashup is a website that uses Google Maps within its site to show visually where an address exists. So there is a web page with an address on it that becomes much more useful when another application, Google Maps, is combined with it to provide further information.

For example, see below an example from a real estate site that uses Google Maps to show the locations of local neighborhoods.

mashup

How to Enable and Disable JavaScript in Mozilla Firefox

Posted in JavaScript, Technology with tags , on September 26, 2007 by Joey

JavaScript can easily be enabled and disabled in Firefox through a check box that is found at the following location in Mozilla Firefox:

  • Click ‘Tools’ on the menu bar
  • Click ‘Options’ from the menu
  • Click on the ‘Content’ section in the pop up box
  • Click the ‘Enable JavaScript’ check box to enable JavaScript; remove the check in the box to disable JavaScript

Related articles:
How to Enable and Disable JavaScript in Internet Explorer

JavaScript Instance Properties

Posted in JavaScript, Software, Technology, internet, programming on September 26, 2007 by Joey

A constructor that is created in JavaScript can have properties defined for it. Each instantiation of the constructor has it’s own copy of the properties. These are called ‘instance properties.’

For example, suppose the following constructor was defined:

function Dog()
{
this.legs = 4;
}

From this constructor, two instances of the Dog class could be instantiated:

var myDog = new Dog();
var myDog2 = new Dog();

Each of these instances has the same value for the ‘legs’ property but they eahch have their own copy.

document.write(myDog.legs);
document.write(myDog2.legs);

Related articles:
Object-oriented JavaScript
JavaScript Instance Methods

Ajax Polling

Posted in JavaScript, Software, Technology, Web, Web 2.0, internet, programming on September 25, 2007 by Joey

Ajax polling is when a browser makes an asynchronous request to a server at regular intervals to see if anything has changed on the server. When it is determined that a change has been made, the page can be updated accordingly.

For example, a page showing product information which includes quantity available, could be polling the server consistently to see if the quantity available has changed. If so, that can be immediately reflected on the page, without any interaction from the user. This simple technique has a multitude of uses – it’s simply up to designers and developers to use their imaginations and find the most appropriate places to use this tool.

JavaScript is an Object-Oriented Language

Posted in JavaScript, Software, Technology, programming on September 24, 2007 by Joey

JavaScript is often considered to not be an object-oriented language. This stems from it not supporting inheritance through classes. JavaScript does use prototype-based inheritance, however. Prototypes make properties and methods of a constructor available to all instantiations of the constructor. These aren’t classes, in the strict sense of the word, but JavaScript is able to simulate classes effectively.

Related articles:
Object-Oriented JavaScript

JavaScript Prototypes

Posted in JavaScript, Technology, internet, programming on September 20, 2007 by Joey

In JavaScript, when a function is defined a property called prototype is automatically created. Properties and methods can be added to this prototype. Any properties or methods added to this prototype will be available to any instantiated instances of the function. The function is a constructor and therefore is similar to a object-oriented class.

For instance, if a constructor function exists for a square, as follows:

function Square(intSideLength)
{

this.perimeter = 4*intSideLength;

this.area = 2*intSideLength;
}

Then a property can be added to all instances of the Square class, as follows:

Square.prototype.sides = 4;

Since this property has been set up, it can be accessed on all instantiated instances of the Square class. For example:

var mySquare = new Square(5);
document.write(mySquare.sides);

Methods can also be added to prototypes:

function Square(intSideLength)
{
this.sideLength = intSideLength;
}

Square.prototype.perimeter = function() {return 4*this.sideLength;}

var mySquare = new Square(5);
document.write(mySquare.perimeter());

Related articles:
Object-oriented JavaScript