2016-03-08 89 views
0

無法弄清楚。我如何獲得NaNnullvolume1volume2屬性?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>

+1

看來好像你可能試圖擴展內置對象('Set'),這可能在你正在使用的實現中不可能... –

+1

嘗試在getter中記錄'this',你會驚訝地發現它不是'Set'對象,意思是'this.repCount'等等。 undefined – adeneo

回答

1

jQuery的extend不會複製的getter/setter - https://bugs.jquery.com/ticket/6145

沒有它那種工作的get

function Set(defaults) { 
    $.extend(this, { 
     repCount: undefined, 
     weight: undefined, 
     volume1: function() { 
      return this.repCount * this.weight; 
     } 
    }); 

    if (defaults) { 
     $.extend(this, defaults); 
    } 
} 

$.extend(Set.prototype, { 
    volume2: function() { 
     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()); 
0
Object.defineProperty(Set.prototype, "volume2", { 
    get: function() { 
     return this.repCount * this.weight; 
    }, 
    enumerable: true, 
    configurable: true 
}); 
+0

這被明確標記爲已棄用你連接的問題? – Bergi

+1

對不起...更新 –

2

做了一些調試和我看到了你的getter方法是使用「undefined」的實例在你的構造函數中多出兩個。所以undefined * undefined會返回NaN。我改變了腳本構造Set對象的方式以使其起作用,它現在返回一個數字作爲構造函數中設置的對象的屬性,並且不具有未定義的默認值。

你可以通過設置你的document.write來獲取volume1,改變你的getter來返回this.repCount,並且設置repCount爲0或者undefined,這樣做就像我在這裏所做的那樣:

undefined = ''[0]; 

     function Set(defaults) { 
      $.extend(this, { 
       repCount: 0, 
       weight: 0, 
       get volume1() { 
        return this.repCount; 
       } 
      }); 

      if (defaults) { 
       $.extend(this, defaults); 
      } 
     } 

     $.extend(Set.prototype, { 
      get volume2() { 
       return this.repCount; 
      } 
     }); 

     var firstSet = new Set({ 
      weight: 135, 
      repCount: 8 
     }); 

     document.write(JSON.stringify(firstSet)); 
     document.write("<br />"); 
     document.write(firstSet.volume2); 

這裏的工作代碼:

 undefined = ''[0]; 

     function Set(weight, repCount) { 
      this.weight = weight; 
      this.repCount = repCount; 


      $.extend(this, { //it using the current instance as the return 
       get volume1() { 
        return repCount * weight; 
       } 
      }); 


     } 

     $.extend(Set.prototype, { 
      get volume2() { 
       return this.weight; 
      } 
     }); 

     var firstSet = new Set(135, 8); 

     document.write(JSON.stringify(firstSet)); 
     document.write("<br />"); 
     document.write(firstSet.volume1); 
1

正如@pkyeck提到,jQuery的extend不會複製getter/setter方法。之後你必須添加這些屬性。

volume1你可以試試這個:

Object.defineProperty(this, "volume1", { 
    get: function() { 
    return this.repCount * this.weight; 
    } 
}); 

對於volume2

var p = Set.prototype; 
Object.defineProperty(p, "volume2", { 
    get: function() { 
    return this.repCount * this.weight; 
    } 
});