Introduction
Objects
Instances
Local
Inheritance
Prototypes
Override
Overwrite
Protection
Arguments
Constructors
Methods
Proto
Arguments Array
Callee
New
Extend
Super
Glossary
 
 
Debreuil Digital Works © 2001
 

CLASSES

Forget what you know about functions. Forget that they do a bunch of stuff and return an answer. Forget that they are a way to reduce a block of code to an easy name. Even forget the Alamo. Functions, in fact, are just templates used to set properties in an object. The keyword here is templates. This template can be used to make new objects, and it can also be used to modify existing objects. You can make as many objects as you like with the same template.

You cannot eat, touch or smell a template itself, only the objects you make with it. It can be useful to think of templates as ideas, and objects as things in the real world, like your shoe... You can not eat the idea of shoes, but you can eat your shoe. That should clear things up if you were confused.

This is all getting a bit abstract, however once again it's a very simple concept. Here is an example:

Template = function()
{
  this.x = 5;
  this.y = 7;
}
   
inst1 = new Template( );
inst2 = new Template( );
inst3 = new Template( );
   
trace( inst1.x); // 5
trace( inst1.y); // 7
trace( inst2.x); // 5
trace( inst2.y); // 7...

Capitalization Tip

Classes should always start with a capital letter. Instances should always start with a lowercase letter. If you stick with this rule, it will always be easy to tell them apart.

In spite of the fact that a class is merely an abstract idea in the ether, and an instance is so real it leaves a mark when it slaps you, they tend to look eerily similar in a program.

Remember: ClassName - instanceName

The word used in OO terminology for these templates is 'Class' (think 'classify' and 'classification', not 'classroom').

A Class is a template that is used to make new objects, and these objects are called 'instances'. Objects and instances are one and the same thing, and can be used interchangeably (and they often are!). That being said, the word 'instance' usually suggests objects that were created from a class using the new operator, while 'object' is a more general term for all objects.

Because instances come from classes, and classes create instances, they are easier to understand when discussing them both at once, so lets move on to the next section, instances...

 

Objects < < Home > > Instances