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
Functionobject’sprototype. It doesn’t belong in a class. It’s not a method of your class.The
Functionobject is basically a built-in type, and itsprototypeis sort of its base class. By extending itsprototypeby addingbindall objects that inherit fromFunction, which is all functions including the ones you defined, will have abindmethod 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.