View Full Version : In explanation
grandsp5
September 12th, 2003, 07:51 PM
Can someone explain a little bit about the operator in. Does it just specifty which directory flash should look in?
senocular
September 12th, 2003, 07:56 PM
the wha?
grandsp5
September 12th, 2003, 08:05 PM
buttonsEnabled = function (value) {
for (var prop in _root) {
if (_root[prop] instanceof Button) {
_root[prop].enabled = value;
}
}
};
// enable buttons
buttonsEnabled(true);
// disable buttons
buttonsEnabled(false);
This is a little something Kode wrote. What does the "in" do. I was guessing that the for loop looks through all the variables located inside root, checks to see if they are buttons, and if they are, disables or enables them. I was interested in how this works, particularly the for loop which seems to be missing some parts.
senocular
September 12th, 2003, 08:17 PM
well _root isnt exactly a "directory" but rather a "scope." A scope is the object or timeline (which itself is a movieclip object) in which a value exists. For example, if you wrote that buttonsEnabled function in _root, its scope would be _root since it 'lives' or was created in _root. because its scope is _root, to access that function if not in _root, you'd specify _root ie
_root.buttonsEnabled(value);
What in is, is just a small part of a larger operation, the for..in loop. The for..in loop is somewhat like your typical for or while loop only that, instead of specifying some condition like i<count; the for..in loop just goes through all the properties, values, functions etc that 'live' in a certain scope.
So what that code is doing is looping through everything that lives in _root and assigning it to a prop variable. That is then referenced in _root to get an object reference using associative array syntax (_root[prop]) to check to see if the current prop is a button or not. If it is, it makes it enabled based on the passed value.
grandsp5
September 12th, 2003, 08:18 PM
right. thanks sen. makes total sense. its just a different type of for loop. So it searches by scope not by level or depth?
senocular
September 12th, 2003, 08:35 PM
by scope.
you can use it on a timeline or movieclip, the Math object, the _global object or any object you decide to make on your own. If you want to know what properties you saved in that object, use a for..in loop and it will go through them all
grandsp5
September 12th, 2003, 08:37 PM
gotcha. thanks
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.