Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7436065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:10:44+00:00 2026-05-29T10:10:44+00:00

How can I access instance variables in closure(inner function) for this style: this[‘varName’] ?

  • 0

How can I access instance variables in closure(inner function) for this style: this['varName']?

public class Test extends Sprite
{
    private var a0:String = 'a0';
    private var a1:String = 'a1';
    private var a2:String = 'a2';

    public function Test()
    {
        var testFun:Function = function(evt:Event):void
        {
            for(var i:uint = 0; i < 3; i += 1)
            {
                trace(this['a'+i]);//how to access instance variables?
            }
        };
        this.addEventListener(Event.ENTER_FRAME, testFun);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:10:45+00:00Added an answer on May 29, 2026 at 10:10 am

    Square brackets can be used to access properties of an Object (everything) exactly as you’ve done above:

    object[propertyName]
    

    Or set properties, assuming that your class is either dynamic or already has the property you’re trying to set:

    object[propertyName] = 10;
    

    With an Object, you’re actually able to use a for(String in Object) loop to output all of the properties you want:

    var object:Object = {
        test: "string",
        another: 5
    };
    
    for(var i:String in object)
    {
        trace(i + ": " + object[i]);
    }
    

    As for your current code, restructure it:

    public class Test extends Sprite
    {
        private var a0:String = 'a0';
        private var a1:String = 'a1';
        private var a2:String = 'a2';
    
        public function Test()
        {
            testFun();
        }
    
        public function testFun():void
        {
            for(var i:uint = 0; i < 3; i += 1)
            {
                trace(this['a'+i]);
            }
        }
    }
    

    If you must retain your inner function, parse a reference to your current instance of Test into it:

    var testFun:Function = function(subject:Test):void
    {
        for(var i:uint = 0; i < 3; i += 1)
        {
            trace(subject['a'+i]);//how to access instance variables?
        }
    };
    

    And then:

    testFun(this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I observed that Outer classes can access inner classes private instance variables. How is
How can I access a controller instance from the view? E.g. I have a
How can hibernate can access a private field/method of a java class , for
does anyone know where on access 2007 a function where i can access my
I have some instance variables in my class that I'd want to be accessible
I have an class with some instance variables. It's purpose is for templating. So
The MSDN documentation says that public class SomeObject { public void SomeOperation() { lock(this)
Trying to understand how CoffeeScript instance and class variable works I came with this
If I have a class Person which has two instance variables: name of type
I've got a partial which should access the instance variables of various Controllers based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.