| |
|
||||
|
Arguments Properties that are general properties of a whole class should be set in the class's prototype. OK, but then what use is the block of code that runs when we create an instance? In previous examples we used this block to set values on the instance, but these were constant values that were the same for each instance. If they are the same for every instance, they probably belong to the class - this is why we moved the 'legs' property to the prototype after all. But if we could only set constant values with this block of code under the class, then indeed, it would have a pretty limited use. Fortunately, as you no doubt are aware of, you can pass values to functions (and thus classes). These can be used to make each call to the class behave a little differently. The call to the class comes from the instance-to-be, so it logically knows its own special needs. The following example may look like a normal function, but follow it as a process of creating an object - the nameless box having properties attached to it, then being assigned to a property. Note this new object will also pick up properties from the prototype of the class. Dog = function( age, hair ) { this.age = age; this.hair = hair; } Dog.prototype.legs = 4; rover = new Dog( 6, "shaggy" ); fido = new Dog( 4, "puffy" ); yeller = new Dog( 12, "gray" ); A few things are worth noting here. First, notice that the names of the arguments are the same as the names assigned to the properties of the instance (this.age = age). You might feel that in order to avoid name collisions, it would be better to use argument names like passedAge and passedHair. If you take a close look however, you can see that this isn't necessary. this.age refers to the property called age in the instance. Using the word age by itself refers to a property called age in the activation object (the {...} part). You can see that setting var age = 6 inside this block of code would overwrite the argument age, but not this.age. It is important to understand these namespaces, so go over it again and try a few test files of your own if you find this a bit fuzzy. The second important, if somewhat obvious point, is that arguments are assigned based on their position, and not anything else. If you pass the above Dog class two properties - hair and age *in that order* - the order will not be adjusted for you even though your class uses the same names (age and hair) in a different order. The argument age always gets the first value passed, and the argument hair always gets the second, as that is the order they are delcared in the class, which takes precedence. Here is an example based on an old Herman cartoon (I owe Jim Unger something here!) marylin = new Date(36,22,35); bridgette = new Date(38,23,36); herman = new BlindDate(42,18,37); but when you look at the classes: function Date( bust, waist, hips ) { this.bust = bust; this.waist = waist; this.hips = hips; } so far so good... function BlindDate( age, IQ, shoeSize ) { // uh ohhh... this.age = age; this.IQ = IQ; this.shoeSize = shoeSize; } ...clearly the class gets the values only, and the class decides what these values mean (!). The last obvious point to make is that arguments are passed by 'what they evaluate to' - meaning that if you pass 'arg' and 'arg' is equal to 2, then you are passing 2. Of course, if 'arg' refers to an object, you are passing a reference to that object, so then changes will be reflected in the original object - but the point being that it is what 'arg' evaluates to that matters, not the name 'arg' itself. Look at the following (not very OO!) code: function test(arg1, arg2) arg1 = "original";
arg2 = { prop1:5 }
test( arg1, arg2 ); trace( arg1 ); // "original" - did not change trace( arg2.prop2 ); // 6 - did change The function will only receive the 'value' of what is passed, in the sense of 'what it evaluates to'. Of course you always knew this, and after that explanation you may even still understand it. That's good, because this applies to classes too, as well as methods, which we will cover shortly. So, to summarize: The instance calling the class must check the class for what arguments it wants, as well as the order it wants them in. Arguments are passed by value. We will cover these arguments in a little more depth in the Arguments[] section (the arguments array, that is). Protection < < Home > > Constructors |