View Full Version : reserve words in parameters
joshchernoff
May 26th, 2007, 03:13 AM
test_mc.addEventListener(MouseEvent.CLICK, test);
function test(event:MouseEvent):void{
trace(event);
}is there a reason other then convention to use the word event in the functions parameter, because any other name would work too. example.
test_mc.addEventListener(MouseEvent.CLICK, test);
function test(traceEvent:MouseEvent):void{
trace(traceEvent);
}the only reason I ask is because event turns blue when I use it and that usually means it's a reserved word. so what is it?
senocular
May 26th, 2007, 08:34 AM
blue doesn't mean "reserved" as much as it means its been used somewhere else by some other class or command. Using event is fine but so is anything else you want to use. A real list of reserved words should be in help.
Rezmason
May 26th, 2007, 09:53 AM
Just make sure that the reserved word you're using isn't a property in the current scope. For instance, you clearly can't make a new variable "width" in a class that extends Sprite.
senocular
May 26th, 2007, 09:55 AM
You can in a parameter list, though.
Rezmason
May 26th, 2007, 11:27 AM
Is that because the parameter is really a property of the Function object?
I notice that if you call one function from another, and you're using parameters with the same name as variables in a higher scope, you get interesting results:
var bool:Boolean = true;
function f():void
{
trace(bool);
}
function g(bool:Boolean=false):void
{
trace(bool);
f();
}
g();
// output:
// false
// true
senocular
May 26th, 2007, 11:59 AM
parameter values are defined in the local scope of a function. When you reference variables by name in a function, the function will first look to its own local scope for a variable with that name and if found, return its value. If not found, the variable value of that name within the current scope is retrieved. Each function, when called, has its own local scope. In your example, g sees bool as false because bool is a local variable in that function whose value is false. The call to f has no local variables so it uses the variable named bool in the current scope whose value is true.
Krilnon
May 26th, 2007, 12:12 PM
The parameters are stored in an internal, so-called activation object that binds them to the function's scope.
Whether or not something is interesting is completely subjective, but what is unusual or unexpected about those results? g's bool parameter is local to g, so it makes sense that that local value would be the one used when it isn't prefixed with another object. f is called within g, but it's still bound to the scope that it was defined in, which wasn't within g's scope.
Rezmason
May 26th, 2007, 12:23 PM
The results were unexpected because it's intuitive that one function called within another function would somehow take into account the scope of the call instruction. On second thought, that would be an unlikely characteristic of an object-oriented language, since one class will often call methods of other classes, and the scope of the call instruction shouldn't be considered.
Krilnon
May 26th, 2007, 06:51 PM
The results were unexpected because it's intuitive that one function called within another function would somehow take into account the scope of the call instruction.
It seems like that would become messy pretty quickly. When function definitions are nested, it makes sense that a child function could access its parent's scope, as those identifiers are always going to be present (even if they're undefined or null). When calling already defined functions, even if you're just looking at the functional side of AS (as in, disregarding that it is also OO), it would be pretty nonsensical to have to be careful when choosing names for identifiers just in case one of the functions that is called several levels down into the call would happen to have an identically named identifier. That would especially troublesome if you weren't the developer of all of the called functions.
Also, imagine that what you were thinking originally was actually the case. If you passed a mapping function to Array.map for instance, then you would have to be sure that map didn't use any identifiers with names that were the same as the ones that you used in your mapping function, but this would be impossible, as you don't have the source to the Array class. If you did, it wouldn't be feasible for you (or a compiler/interpreter) to search through so many places to see if the identifier was used anywhere.
joshchernoff
May 26th, 2007, 07:28 PM
so what this all comes down to is that a parameter is only a parameter and it can be named what ever you like?
senocular
May 26th, 2007, 11:17 PM
so what this all comes down to is that a parameter is only a parameter and it can be named what ever you like?
not whatever, you cannot name it a reserved word. Note that existing properties are not reserved, they're just "used." Reserved words include things like for, try, do, if, function, and things like that - actual language words, not named properties. Again, help should have a list of these
joshchernoff
May 26th, 2007, 11:22 PM
not whatever, you cannot name it a reserved word. Note that existing properties are not reserved, they're just "used." Reserved words include things like for, try, do, if, function, and things like that - actual language words, not named properties. Again, help should have a list of these
oops my bad, thanks for clearing that up.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.