Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.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
设置内部对象javascript中字段的值_Javascript_Function_Scope - Fatal编程技术网

设置内部对象javascript中字段的值

设置内部对象javascript中字段的值,javascript,function,scope,Javascript,Function,Scope,我有一个名为light的类,它通过ajax加载其状态: function Light(name, channel, type, state) { this.name = name; this.channel; this.type=type; this.state = state; //0=off 1=on this.turnOn = function () { this.state = 1; $('#luce-' + name).attr('src', controlIma

我有一个名为light的类,它通过ajax加载其状态:

function Light(name, channel, type, state) {
this.name = name;
this.channel;
this.type=type;
this.state = state;   //0=off  1=on
this.turnOn = function () {
    this.state = 1;
    $('#luce-' + name).attr('src', controlImagePath + '' + type + '100.png');
}
this.turnOff = function () {
    this.state = 0;
    $('#luce-' + name).attr('src', controlImagePath + '' + type + '00.png');
}
this.toggle = function () {
    if (this.state == 0) this.turnOn();
    else this.turnOff();
}
this.checkState = function () {
    var result;
    $.jsonp({
        callback: "callback",
        url: 'http://' + serverAddress + ':' + serverPort + '/GetChannelValueAsJsonp?channelName=' + channel + '&callback=?',
        success: function (data) {
            if (data > 0) {
                 this.turnOn();
            } else {
                 this.turnOff();
            }
        },
        error: function (xOptions, textStatus) {
            console.log("error");               
        }
    });
}
}

它不断地给出错误:

Uncaught TypeError: Object #<Object> has no method 'turnOn'
uncaughttypeerror:对象#没有方法“打开”
我怀疑这是因为success函数覆盖了Light scope。如何从另一个功能范围引用对象?
IE在Java中我会做Light.this.Turn()。。。如何使用javascript实现这一点?

谢谢

这个
在XHR回调函数中引用jQuery/XHR对象。您必须将
关键字保存在变量中:

this.checkState = function () {
    var result;
    var $this = this;  //Saving `this`
    $.jsonp({
        callback: "callback",
        url: 'http://' + serverAddress + ':' + serverPort + '/GetChannelValueAsJsonp?channelName=' + channel + '&callback=?',
        success: function (data) {
            if (data > 0) {
                 $this.turnOn(); //Referring to `this` through $this`
            } else {
                 $this.turnOff();
            }
        },
        error: function (xOptions, textStatus) {
            console.log("error");               
        }
    });
}

这个
在XHR回调函数中引用jQuery/XHR对象。您必须将
关键字保存在变量中:

this.checkState = function () {
    var result;
    var $this = this;  //Saving `this`
    $.jsonp({
        callback: "callback",
        url: 'http://' + serverAddress + ':' + serverPort + '/GetChannelValueAsJsonp?channelName=' + channel + '&callback=?',
        success: function (data) {
            if (data > 0) {
                 $this.turnOn(); //Referring to `this` through $this`
            } else {
                 $this.turnOff();
            }
        },
        error: function (xOptions, textStatus) {
            console.log("error");               
        }
    });
}

谢谢,我知道这是完全不正确的,但还是找不到路@andreapier我使用了一个随机变量名,
$this
。它应该被定义为
this
,而不是
$(this)
。是的,我知道,我是指我以前写的东西。谢谢,我知道这是完全错误的,但仍然找不到方法@andreapier我使用了一个随机变量名,
$this
。它应该被定义为
this
,而不是
$(this)
。是的,我知道,我指的是我以前写的东西。