在对象“意外的令牌this”中使用“ this”

时间:2018-06-27 21:29:54

标签: javascript object this

我正在使用DOMString对象来设置DOM。我使用this指向init,但显示为Unexpected token this。我正在学习javaScript。请帮助我如何使用它。非常感谢。

let imageView = {

init: function () {
    // store pointers to our DOM elements for easy access later
    let DOMstrings = {
        containerElem: '#cat',
        nameElem: '#cat-name',
        imageElem: '#cat-img',
        countElem: '#cat-count'
    },
    this.catElem = document.getElementById(this.DOMstrings.containerElem);
    this.catNameElem = document.getElementById(this.DOMstrings.nameElem);
    this.catImageElem = document.getElementById(this.DOMstrings.imageElem);
    this.countElem = document.getElementById(this.DOMstrings.countElem);
};

1 个答案:

答案 0 :(得分:2)

DOMstrings对象后有一个逗号,该逗号应为分号。

尝试一下:

let imageView = {

    init: function () {
        // store pointers to our DOM elements for easy access later
        let DOMstrings = {
            containerElem: '#cat',
            nameElem: '#cat-name',
            imageElem: '#cat-img',
            countElem: '#cat-count'
        };
        this.catElem = document.getElementById(DOMstrings.containerElem);
        this.catNameElem = document.getElementById(DOMstrings.nameElem);
        this.catImageElem = document.getElementById(DOMstrings.imageElem);
        this.countElem = document.getElementById(DOMstrings.countElem);
    }