• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Scope of vars, and scope of functions

Participant ,
Apr 21, 2023 Apr 21, 2023

Copy link to clipboard

Copied

It seems that a var defined at the top of any frame, outside of a function, is visble to all frames. It that the case? If so, if the same variables are reassigned new values in each frame, is it better to declare them all in frame1?

 

Also, it seems that functions declared in one frame are seen as duplicate if used again in another frame. While it's fine it my application to share vars across frames, should functions all be declared as private to limit their scope to the frame in which they're located?

Views

323

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 21, 2023 Apr 21, 2023

variables should only be declared once. they can be assigned values whenever and wherever you want.

 

with functions it's more complicated because there are anonymous functions and there are named functions.

Votes

Translate

Translate
Community Expert ,
Apr 21, 2023 Apr 21, 2023

Copy link to clipboard

Copied

variables should only be declared once. they can be assigned values whenever and wherever you want.

 

with functions it's more complicated because there are anonymous functions and there are named functions.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 21, 2023 Apr 21, 2023

Copy link to clipboard

Copied

It seems I have to give every function a unique name, even if it's to add the a listener to another instance of a single button. Is that the most efficient way to do that. What are the best practices for useing the same function on mutliple branches of an interface? For example:

cb_1.addEventListener(Event.CHANGE,cb_1F);
function cb_1F(e:Event):void{

on frame2

On frame 4 needs to change to:

cb_1.addEventListener(Event.CHANGE,cb_2F);
function cb_2F(e:Event):void{

 

Is that true?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2023 Apr 21, 2023

Copy link to clipboard

Copied

LATEST

define a function once and never again.

 

a little background follows.

 

anonymous function is most commonly used and looks like:

 

function something(){

 

}

 

a named function is:

 

var something = function(){

 

}

 

they are different in sever key ways, but to simplify things, lets just discuss anonymous functions.

 

if you define an anonymous function on frame one hundred, you can call it on frame 1 (and every other frame on the the same timeline).  frame 100 doesn't even have to play, ever.  you can even burry an anonymous function definition on a movieclip's frame 100 and call it from the main timeline's first frame.

 

with your combobox, example, either add the same listener to all comboboxes (if the same listener function works for them all:

 

cb_1.addEventListener(Event.CHANGE, cbF);

cb_2.addEventListener(Event.CHANGE, cbF);

// etc

 

and you can define your listeners on any frame(s) and call the same function in a different frame.

 

function cbF(e:Event):void{
trace(e.currentTarget.selectedItem.label);
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines