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?
-
You're extending the
Function
object'sprototype
. 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 itsprototype
is sort of its base class. By extending itsprototype
by addingbind
all objects that inherit fromFunction
, which is all functions including the ones you defined, will have abind
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.
pkaeding : 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?apphacker : 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!pkaeding : 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.apphacker : I edited the answer with link to a similar question and a better answer than I gave.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.