Tutorial details:
Written by: Michael Barry
Author Email:
Difficulty Level: intermediate
Author Website: http://www.iol.ie/~michaelbarry/public_html
Version: Flash 5. Prototypes and inheritance.

When you create a function you also create a 'prototype' of the function. Even if the prototype is not defined explicitly it exists as a 'template' for the function and any new instances of that function. When you want to add properties to the new instances of the function you can explicitly define these properties using 'prototype'. e.g.

myFunction.prototype.y = "v";

We now have defined a property, y, with a value "v".
This property and value can be inherited by any new instances of the function:

//define a function;
 function myFunction(a){
//create an instance of this function with the 'new' operator;
 var myNewFun = new myFunction();
//define the prototype and create a properties 'x', 'y' and 'z' in the prototype;
 myFunction.prototype.x = "Create";
 myFunction.prototype.y = "a";
 myFunction.prototype.z = "sentence";

Now access these properties through the new myFunction, namely 'myNewFun'.

var output = myNewFun.x+" "+myNewFun.y+" "+myNewFun.z;

output = "Create a sentence"

What happens here is that Flash searches 'myNewFun' for the property 'x'. It does not find it because we have not explicitly stated it in that instance, so it goes to the original function "myFunction" and does not find it there so it goes to the prototype and finds it there and adds its value to output.
The same process happens for y and z and their values are added to 'output'. If a value is found in the new instance it takes precedance over values in the function itself or its prototype. This is called 'shadowing' where a value shadows another value deeper in the chain.

This process gives rise to the notion of 'order of precedence' with regard to properties in each section of the function chain. This order is; myNewFun; -- original myFunction; -- myFunction.prototype.

For example, suppose we were to define our function chain as follows;

myFunction.prototype.x = "Create";
 myFunction.prototype.y = "a";
 myFunction.prototype.z = "sentence";
function myFunction(){
 this.x = "Make";
 this.y = "I'm shadowed by y in 'myNewFun'";
var myNewFun = new myFunction();
 myNewFun.y = "a shadow";
var output = myNewFun.x+" "+myNewFun.y+" "+myNewFun.z;

output = "Make a shadow sentence";

This is how the sentence is created:
x is not found in myNewFun.
x is found in myFunction so this value ("Make") is inserted in output and the x value in the prototype ("Create") is shadowed.
y is found in myNewFun so "a shadow" is chosen instead of the y value in myFunction or the y value in its prototype.
z is not found in myNewFun or myFunction but is found in myFunction.prototype so "sentence" is appended to 'output'.


Properties can also have methods as values and these methods can be functions. e.g.

function myCirc(){
function myArea(r){
 return r*r*3.1415;
a=new myCirc();
 var output=a.x(12);

output = 452.376

So what is happening here?
1.var output=a.x(12);
// The argument, 12, is associated with a property 'x' in 'a'.
2. 'a' is a variable which references a new instance of 'myCirc'.
3. The property 'x' is not explicitly defined for 'a' in terms of the reference 'a'.
// I could have written: a.x = aValue; but this is not found for x in a.
4. The original function myCirc does contain a property 'x'. ('this.x')
5. This property is a method, myArea and it computes the area of a circle with radius 12.

So here is the path of the argument '12'.
No explicitly defined property 'x' exists for 'a'.
So the original 'myCirc' is checked for the property x.
It has been defined in myCirc: 'this.x', and it is a function - myArea.
So output = myArea: return 12*12*3.1415 = 452.376.


If you want to check which properties belong to an object you can check them like this;

var output="";
 function myFunction(){
var mm=new myFunction;
function listProps(){
 for(var i in mm) output+=i+"="+mm[i]+".";

output = "x=aaa.y=bbb.z=ccc."