我正在使用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);
};
答案 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);
}