Closed Wu-Hao closed 11 years ago
Using Object.bind() is the obvious choice.
Space.addCollisionHandler(3,8, null, box1.sayHi.bind(box1));
Or:
Space.addCollisionHandler(3,8, null, function() { box1.sayHi(); });
Just a small clarification, it's not actually Object.bind, but rather Function.prototype.bind. bind
is a method on the Function
prototype, not the Object
prototype. Object
inherits its prototype from Function
.
It doesn't change @josephg's answer, in that bind
is the right choice, but if someone doesn't know what bind
is, then it's important to start with accurate information... which I hopefully provided :)
Thanks - I'm so used to coffeescript where you just use the fat arrow operator.
Yeah, I understand. It's definitely one of JS's awesome-powers that can also shoot you in the foot.
For what it's worth, I'm very glad you didn't write this library in coffee-script!
Alert is called
But real world application requires a target for function to call
you get undefined, because what ever object calling "sayHi()" it does not have .hi attribute
Any idea how to fix this? In cocos2d-html5, our callback handler function accepts a function with a target like