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

  • Home
  • SEARCH
  • 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 6549349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:04:55+00:00 2026-05-25T12:04:55+00:00

I’m writing some Actionscript3 code that attempts to apply a method to an object

  • 0

I’m writing some Actionscript3 code that attempts to apply a method to an object that is determined at runtime. The AS3 documentation for Function.apply and Function.call both indicate that the first argument to those functions is the object which will be used as the ‘this’ value when the function is executed.

However, I have found that in all cases when the function being executed is a method, the first parameter to apply/call is not used, and ‘this’ always refers to the original object to which that method was bound. Here is some example code and its output:

package
{
    import flash.display.Sprite;    
    public class FunctionApplyTest extends Sprite
    {
        public function FunctionApplyTest()
        {
            var objA:MyObj = new MyObj("A");
            var objB:MyObj = new MyObj("B");

            objA.sayName();
            objB.sayName();

            objA.sayName.apply(objB, []);
            objA.sayName.call(objB);
        }
    }
}

internal class MyObj
{
    private var _name:String;
    public function MyObj(name:String)
    {
        _name = name;
    }   
    public function sayName():void
    {
        trace(_name);
    }
}

Output:

A
B
A
A

A minor modification to the above code to create an in-line anonymous function which refers to ‘this’ shows that the correct behavior occurs when the function being applied/called is not a bound method.

Am I using apply/call incorrect when I attempt to use it on a method? The AS3 documentation specifically provides code for this case, however:

myObject.myMethod.call(myOtherObject, 1, 2, 3);

If this is indeed broken, is there any work-around besides making the target methods into functions (which would be quite ugly, in my opinion)?

  • 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-25T12:04:56+00:00Added an answer on May 25, 2026 at 12:04 pm

    Its not a “bug”, but the documentation for call and apply is very misleading and doesn’t do a good job at all of explaining whats going on. So here is an explaination of what is happening.

    Methods are different from Functions in ActionScript. Methods are defined as a part of a class defintion, and methods are always bound to that instance. See the Methods second of this link. To quote from there:

    Methods are functions that are part of a class definition. Once an instance of the class is created, a method is bound to that instance. Unlike a function declared outside a class, a method cannot be used apart from the instance to which it is attached.

    So when you make a new instance of MyObj, all of its methods are bound to that instance. Which is why when you try to use call or apply, you aren’t seeing this getting overridden. See the section on Bound Methods for details.

    See, this document for an explanation of the traits object, which actionscript uses to resolve methods and used for performance reasons behind the scenes is probably to blame. That or class methods are just syntactic sugar for the following ECMAScript pattern:

    var TestClass = function(data) {
        var self = this;
        this.data = data;
        this.boundWork = function() {
            return self.constructor.prototype.unboundWork.apply(self, arguments);
        };
    };
    
    TestClass.prototype.unboundWork = function() {
        return this.data;
    };
    

    Then:

    var a = new TestClass("a");
    var b = new TestClass("b");
    
    alert(a.boundWork()); // a
    alert(b.boundWork()); // b
    
    alert(a.unboundWork()); // a
    alert(b.unboundWork()); // b
    
    alert(a.boundWork.call(b)); // a
    alert(a.boundWork.call(undefined)); // a
    
    alert(a.unboundWork.call(b)); // b
    

    or even more interesting:

    var method = a.unboundWork;
    method() // undefined. ACK!
    

    Vs:

    method = a.boundWork;
    method() // a. TADA MAGIC!
    

    Notice that boundWork will always get executed in the context of the instance it belongs to, no matter what you pass in for this with call or apply. Which, in ActionScript, this behavior is exactly why class methods are bound to their instance. So no matter where they are used, they still point at the instance they came from (which makes the actionscript event model a little more “sane”). Once you understand this, then a work-around should become obvious.

    For places where you want to do some magic, avoid the ActionScript 3 based hard-bound methods in favor of prototype functions.

    For example, consider the following ActionScript code:

    package
    {
        import flash.display.Sprite;    
        public class FunctionApplyTest extends Sprite
        {
            public function FunctionApplyTest()
            {
                var objA:MyObj = new MyObj("A");
                var objB:MyObj = new MyObj("B");
    
                objA.sayName();
                objB.sayName();
    
                objA.sayName.apply(objB, []); // a
                objA.sayName.call(objB); // a
    
                objA.pSayName.call(objB) // b <---
            }
        }
    }
    
    internal dynamic class MyObj
    {
        private var _name:String;
        public function MyObj(name:String)
        {
            _name = name;
        }   
        public function sayName():void
        {
            trace(_name);
        }
    
        prototype.pSayName = function():void {
            trace(this._name);
        };
    }
    

    Notice the declaration difference between sayName and pSayName. sayName will always be bound to the instance it was created for. pSayName is a function that is available to instances of MyObj but is not bound to a particular instance of it.

    The documentation for call and apply are technically correct, as long as you are talking about prototypical functions and not class methods, which I don’t think it mentions at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.