如何从ScriptSharp访问JavaScript? [英] How do I access JavaScript this from ScriptSharp?
问题描述
我正在尝试执行以下操作.
I'm trying to do the following.
var handler = e => { handle(); item.Unbind("event", this); }
item.Bind("event", handler);
在JavaScript中这将正常工作,但是ScriptSharp会使用该代码引用包含类的方法的实例来替换JavaScript的this.如何避免这种行为,并从lambda本身获取对lambda的引用?
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?
推荐答案
这是您的操作方法(假设Bind接受具有Action签名的委托):
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.
这篇关于如何从ScriptSharp访问JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!