【发布时间】:2021-03-21 05:35:06
【问题描述】:
我是一个使用 javascript 的新手/来自 PHP 世界/所以请原谅我的无知。
我无法访问 fetchPartial() 中的 this._target 变量,也看不出我做错了什么。
我的错误是TypeError: undefined is not an object (evaluating 'this._target')
你能帮我吗?
'use strict';
class View {
constructor(partial, target) {
this._partial = partial
this._target = target;
}
fetchPartial() {
fetch(this._partial).then(function (response) {
// The API call was successful!
return response.text();
}).then(function (html) {
let elem = document.querySelector( this._target ) // error: TypeError: undefined is not an object (evaluating 'this._target')
elem.innerHTML = html
}).catch(function (err) {
// There was an error
console.warn('Something went wrong.', err);
});
}
}
let p = new View('/_partial.html', '#_partial');
p.fetchPartial();
【问题讨论】:
标签: javascript ecmascript-6 class-variables