Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.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_Ecmascript 6 - Fatal编程技术网

Javascript 我如何保持';这';通过回调和匿名函数在范围内?

Javascript 我如何保持';这';通过回调和匿名函数在范围内?,javascript,ecmascript-6,Javascript,Ecmascript 6,我试图将一些JSON加载到JavaScript类中,并将JSON中的一些属性分配给该类实例上的属性,但我在“this”和作用域方面遇到了问题。我很确定这个解决方案涉及到箭头函数,但我不知道如何将它们组合在一起 以下是我目前掌握的情况: class Test { constructor(url) { this.loadJSON(url, this.init); } loadJSON(url, callback) { let xhr = n

我试图将一些JSON加载到JavaScript类中,并将JSON中的一些属性分配给该类实例上的属性,但我在“this”和作用域方面遇到了问题。我很确定这个解决方案涉及到箭头函数,但我不知道如何将它们组合在一起

以下是我目前掌握的情况:

class Test {

    constructor(url) {
        this.loadJSON(url, this.init);
    }

    loadJSON(url, callback) {
        let xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        xhr.responseType = 'json';
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                callback(xhr.response);
            }
        };
        xhr.send(null);
    }

    init(response) {
        console.log(response.description);
        // this is undefined
        this.description = response.description;
    }

}

在init()方法中,console.log的工作方式与我预期的一样,但是我在为对象分配任何内容时遇到了问题-我只是不断得到“这是未定义的”。

或者修改回调(xhr.response);代码如下:

callback.call(this, xhr.response);
this.loadJSON(url, this.init.bind(this)); 
或者,修改此.loadJsON代码,如下所示:

callback.call(this, xhr.response);
this.loadJSON(url, this.init.bind(this)); 
以下是完整的解决方案:

class Test {


        constructor(url) {
            this.that = this;
            this.loadJSON(url, this.init);
//Or, this.loadJSON(url, this.init.bind(this)); In that case you dont have to modify "callback(xhr.response);" code
        }

        loadJSON(url, callback) {
            let xhr = new XMLHttpRequest();
            xhr.open('GET', url);
            xhr.responseType = 'json';
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    callback.call(this, xhr.response);
                }
            };
            xhr.send(null);
        }

        init(response) {
            console.log(response.description);

            this.description = response.description;

            console.log(this.description);

        }

    }

    let t = new Test('MY URL');

这里有一个很好的解释:使用
this.loadJSON(url,this.init.bind(this))@NanoPish看起来很棒-谢谢!你真的不应该在这里使用
类。它的方法从来没有做过任何有用的事情,它们不应该从外部调用。还有,@Bergi你的评论真的帮助我重新思考了事情。这个链接非常有用,特别是关于“将数据本身作为构造函数的参数,而不是告诉构造函数如何获取数据”的内容。我现在已经对内容进行了重新构造,这更有意义。谢谢那很有效-谢谢!我必须研究一下你的答案和上面@4castle的评论之间的区别。欢迎你