对象有一个属性,用来存数组
通过对象的方法给该属性赋值后打印输出对象,发现有数值,但是属性又是空的,不理解是什么意思
class Position {
#localPlace = [];
constructor() {
this._getLocalPlace();
}
_getLocalPlace() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
this._loadPosition.bind(this),
function () {
alert('获取位置失败');
}
);
}
}
_loadPosition(position) {
const lat = position.coords.latitude;
const lng = position.coords.longitude;
this.#localPlace = [lat, lng];
this.whereAmI();
}
whereAmI() {
// console.log(this.#localPlace);
fetch(
`https://geocode.xyz/${this.#localPlace[0]},${
this.#localPlace[1]
}?geoit=json`
)
.then(response => response.json())
.then(data => {
// console.log(data);
console.log(`你现在在${data.country}的城市${data.city}`);
});
}
test() {
console.log(this.#localPlace);
}
}
const p = new Position();
console.log(p);
p.test();
对象有一个属性,用来存数组
通过对象的方法给该属性赋值后打印输出对象,发现有数值,但是属性又是空的,不理解是什么意思
我都没看懂你想表达什么,什么东西有数值又是空的
有一个属性,哪个属性,变量名是什么