Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.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 将构造函数中的此对象传递给setIntervall_Javascript_Object - Fatal编程技术网

Javascript 将构造函数中的此对象传递给setIntervall

Javascript 将构造函数中的此对象传递给setIntervall,javascript,object,Javascript,Object,好的,我在JavaScript中遇到了一个问题,我创建了一个函数构造函数,并添加了一个属性和方法,当调用一个方法来处理对象环境方法时,作为一个基本示例,因为我的构造函数太复杂了 function Construct(){ this.alert = 'test1'; this.replace = ''; this.interval; this.run = function(){ console.log(this);//echo the constructor this.interval = set

好的,我在JavaScript中遇到了一个问题,我创建了一个函数构造函数,并添加了一个属性和方法,当调用一个方法来处理对象环境方法时,作为一个基本示例,因为我的构造函数太复杂了

function Construct(){
this.alert = 'test1';
this.replace = '';
this.interval;
this.run = function(){
console.log(this);//echo the constructor
this.interval = setInterval(function(){
console.log(this);//echo the window object
alert(this.alert);
this.replace = '';
}
};
}
如果您已经阅读了代码,您必须理解原因,则此操作将失败

如何将构造函数对象(this)传递给set interval函数

我曾尝试使用外部函数并将其作为参数传递,但它失败得很惨,因为replace仍然是原来的样子,而且只有一次是符文为什么

请帮忙


谢谢。

创建一个本地
self
变量,并将其设置为
this
,以便在嵌套函数中使用该变量:

function Construct () {

    this.alert = 'test1';
    this.replace = '';
    this.interval;

    this.run = function () {

        var self = this;

        this.interval = setInterval(function () {
            self.replace = '';
        }, 500);
    };
}

你的意思是self从LL.run函数传输到setIntervall函数谢谢我会尝试它工作了谢谢抱歉迟到我的计算机无法启动