views:

840

answers:

1

I am trying to incorporate prototype.js's bind() function into my Flash component. I found this article by Jordan Broughs, which gave me hope. He suggested using this code snippet:

  Function.prototype.bind = function():Function {
    var __method:Function = this;
    var object:Object = arguments[0];
    return function():void {
   __method.apply(object, arguments);
    }
  }

So, I put that in my class, outside of any methods or constructors. However, when I try to call bind() on a function, I get this compiler error:

1061: Call to a possibly undefined method bind through a reference with static type Function.

Any ideas?

+1  A: 

You're extending the Function object's prototype. It doesn't belong in a class. It's not a method of your class.

The Function object is basically a built-in type, and its prototype is sort of its base class. By extending its prototype by adding bind all objects that inherit from Function, which is all functions including the ones you defined, will have a bind method that creates a closure.

EDIT:

This question is actually a duplicate and has been answered here:

ActionScript problem with prototype and static type variables

And according to that question you have remove the :Function in order for it to work.

apphacker
Okay, great. I was unsure of where to put this code to begin with (I have a lot of experience with javascript, but very little with ActionScript). So, how do I add this code outside of the class? Should it be in the same file as my class? Should it be inside or outside of the 'package' block?
pkaeding
It's should probably be somewhere at the beginning, where your code begins executing. It shouldn't really go inside of any package. I imagine it should go before any attempt to use it, but I haven't used ActionScript in a while enough to know if that will lead to it to not work, I guess you could test!
apphacker
Hmm, I tried putting that code outside my class, but inside the package block, outside the package block, and in its own file, and all give the same error.
pkaeding
I edited the answer with link to a similar question and a better answer than I gave.
apphacker