無法弄清楚。我如何獲得NaN
和null
的volume1
和volume2
屬性?JavaScript對象和getter屬性
undefined = ''[0];
function Set(defaults) {
$.extend(this, {
\t repCount: undefined,
weight: undefined,
\t get volume1() {
\t \t return this.repCount * this.weight;
\t }
});
if (defaults) {
\t $.extend(this, defaults);
}
}
$.extend(Set.prototype, {
get volume2() {
\t return this.repCount * this.weight;
}
});
var firstSet = new Set({
weight: 135,
repCount: 8
});
document.write(JSON.stringify(firstSet));
document.write("<br />");
document.write(firstSet.volume2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
看來好像你可能試圖擴展內置對象('Set'),這可能在你正在使用的實現中不可能... –
嘗試在getter中記錄'this',你會驚訝地發現它不是'Set'對象,意思是'this.repCount'等等。 undefined – adeneo