Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nativescript 在回调中引用self_Nativescript - Fatal编程技术网

Nativescript 在回调中引用self

Nativescript 在回调中引用self,nativescript,Nativescript,我的代码是: class myclass observable.Observable { let label = "test"; navigatingTo(args: observable.EventData) { target.on( "name", this._callback ); } _callback ( eventData ) { console.log( this.label);

我的代码是:

class myclass observable.Observable
{
    let label = "test";

    navigatingTo(args: observable.EventData)
    {
        target.on( "name", this._callback );
    }

    _callback ( eventData )
    {    
         console.log( this.label);
    }
}
当我在回调中打印this.label时,“this”对象不是我期望的对象,我认为应该是myclass实例


我有一个单独的回调方法,因为我稍后还会调用.off(),并且需要对该方法的引用(与匿名函数相反)

使用
on()订阅时,可以传递第三个参数。第三个参数将用作回调的上下文(this)。所以你可能想做:

target.on("name", this._callback, this);

使用
on()订阅时,可以传递第三个参数。第三个参数将用作回调的上下文(this)。所以你可能想做:

target.on("name", this._callback, this);

你能发布你的示例的完整代码(也可以是XML)吗?这将有助于了解整个情况。你能发布你的示例的完整代码(也可以是XML)吗?这将有助于了解整个情况。