How do I access JavaScript this from ScriptSharp?

為{幸葍}努か 提交于 2020-01-06 03:03:04

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!