|
|
|
Protection - Modifying Prototype Properties
 |
You can read properties from a prototype as if they were in the
instance. Any property in an instance will override a property
in its class prototype if it has the same name. You may have noticed
something, though. If we can access properties in the prototype
like they belong to the instance, we should be able to modify
them, right? Let's create a Coyote - maybe you know this one...
This Coyote is so dumb (how dumb is he?) that he has chewed off
three legs and he's still caught in the trap. Here, he loses one
leg at a time, so we probably want to say something like:
Coyote = function( ){}
Coyote.prototype.legs = 4;
ralph = new Coyote( );
ralph.legs--;
ralph.legs--;
ralph.legs--;
|
This
could be a problem. Do you see why? (The coyote still can't run away?
Nope.) The 'legs' property is in the Coyote.prototype,
not in the instance Ralph. When we say
ralph.legs--, we might expect the legs
property in Coyote.prototype to be decremented,
after all, that's where it is. This would create a huge problem for
all other coyotes though, because they get their legs from the same
Coyote.prototype too, which now has the
value 'three' instead of 'four'. One bad apple spoils the lot, indeed.
Well, that is the huge knock against ever putting things in the prototype.
Ha ha, just kidding. In fact, as we touched on before, there is a beautiful
(remember, automatic is beautiful) solution here - whenever a property
from a prototype (legs) gets modified by
an instance (legs--), that property is
first copied into the instance, and then modified. This frees you from
having to tiptoe around properties that are coming from the prototype.
In fact, it saves you from having to know or care where properties are
coming from. If you modify a property in an instance, really what you
are saying is, "this property in this instance is different
now". So it makes sense that these modifications happen in the
instance, even if they came from somewhere else. When you read a property,
however, there is no need to read it from information stored in the
instance if it is a property that belongs to the whole class.
Think
of the sentence "John has big ears", and "John has ears".
In the first one, John's ears have a special "bigness" feature
that sets them apart from normal ears, so we mentally attached this
feature to John. When we just say, "John has ears", however,
John's ears are just like everyone else's, so we would mentally attach
the word ears to "humans", or "people", not to John.
You can experience this mental shift when combining the sentences to
become "John has ears, and they are very big". Your mind took
the ears property from humans, copied it to John, and modified it to
be bigger. Hopefully it did anyway, perhaps your ears are very big and
instead you just took offense. Either way, that whole transaction would
look something like this if your brain ran on Actionscript:
Human= function( ){}
Human.prototype.ears = 4;
.
john = new Human( );
john.ears++;
In this example, the ears property will start in the prototype, and
end up copied into the instance (with the original remaining in the
prototype unchanged). Your brain protects you from assuming all people
will have big ears after meeting John by storing that information with
John, even though ears are an attribute of all humans. Actionscript
does the same thing for you automatically. What about if you accidentally
delete an object that's in the prototype (one that only exists in the
prototype) from an instance? Safe again (it's no coincidence that prototype
and prophylactic are similar looking words). Actionscript will not delete
an object in the prototype, unless you explicitly delete it (by referring
to Human.prototype.ears rather than john.ears
or this.ears). Things are actually very
safe in the prototype; instances can't change these properties unless
they specifically intend to, allowing instances to safely do whatever
they want to themselves. John, in fact, recently chewed off both his
ears (and he still can't get a girlfriend...).
Overwriting
< < Home >
> Arguments
|
|