问题
I'm trying to do the following.
var handler = e => { handle(); item.Unbind("event", this); }
item.Bind("event", handler);
In JavaScript this would properly work, but ScriptSharp replaces JavaScript's this with reference to the instance of class containing method with that code. How do I avoid this behavior and get a reference to the lambda from the lambda itself?
回答1:
Here's how you could do it (assuming Bind takes a delegate with the signature of an Action):
SomeObject item = ...;
Action handler = null;
handler = delegate() {
// do something ... eg. call Handle();
item.Unbind("event", handler);
};
item.Bind("event", handler);
Also, see this question: How to write a function in script# to be called with any object as this, not just with the instance of the class in which it is defined? for a technique for writing code that generates a "this" reference in script.
来源:https://stackoverflow.com/questions/13149273/how-do-i-access-javascript-this-from-scriptsharp