我有一個函數,它本身是一個循環,並且在這個循環中我正在創建一個對象。但是當循環嘗試訪問對象屬性後,我得到了一個未定義的響應。Javascript:對象屬性undefined(非常特殊的情況下)
當我試着只是console.log對象本身,我明白了。問題在於屬性。
如果有人能幫助我,我將不勝感激。我做了一個很好的研究,但沒有找到解決辦法。我的情況很奇怪。除了屬性,一切都很好。我需要高級開發人員的幫助。
這是代碼。
var countMap = {};
var counter = 0;
$('#share_table_body').find('.js-post').each(function() {
var totalCount = 0;
$(this).find('.js-social').each(function() {
var $this = $(this);
var socialType = $(this).data('social-type');
var url = $this.closest('.js-post').data('url');
getShareStatus(url, socialType)
.then(function (shareCount) {
$this.text(shareCount);
if (!countMap[socialType]) {
countMap[socialType] = 0;
}
if (!countMap['total']) {
countMap['total'] = 0;
}
countMap[socialType] += shareCount;
totalCount += shareCount;
countMap['total'] += shareCount;
$this.siblings('.js-total').text(totalCount);
})
.fail(function (err) {
$('.error-notice').removeClass('hidden');
$this.css('color', 'red').text('!');
});
});
counter++;
});
if (counter == $('#share_table_body').find('.js-post').length) {
console.log(countMap);
$('.total-facebook').text(countMap['facebook']);
}
'getShareStatus'包含一個異步函數 – Beginner
這個問題是一個重複的,是的,但更好的回答[爲什麼我的變量沒有改變後,我修改它在一個函數?](http://stackoverflow.com/questions/ 23667086)而不是官方複製通知中引用的問題。 –