【问题标题】:How do I access JavaScript this from ScriptSharp?如何从 ScriptSharp 访问 JavaScript?
【发布时间】:2012-10-30 23:52:04
【问题描述】:

我正在尝试执行以下操作。

var handler = e => { handle(); item.Unbind("event", this); }
item.Bind("event", handler);

在 JavaScript 中 this 可以正常工作,但 ScriptSharp 将 JavaScript 的 this 替换为引用包含该代码的方法的类的实例。如何避免这种行为并从 lambda 本身获取对 lambda 的引用?

【问题讨论】:

    标签: this script#


    【解决方案1】:

    你可以这样做(假设 Bind 接受一个带有 Action 签名的委托):

    SomeObject item = ...;
    Action handler = null;
    
    handler = delegate() {
       // do something ... eg. call Handle();
       item.Unbind("event", handler);
    };
    item.Bind("event", handler);
    

    另外,请参阅这个问题: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?,了解一种在脚本中生成“this”引用的代码编写技术。

    【讨论】:

      猜你喜欢
      • 2010-11-16
      • 2020-06-17
      • 2010-10-30
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多