![](/img/trans.png)
[英]Copy paste online javascript file into own javascript file but the code didn't work. Why?
[英]javascript : why code didn't work
首先,這是我的 javascript 代碼
<script type="text/javascript">
var book = {};
Object.defineProperties(book, {
_year : {
value : 2004
},
edition : {
value : 1
},
year : {
get : function () {
return this._year;
},
set : function (newValue) {
if (newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004;
}
}
}
});
book.year = 2005;
alert(book.edition);
alert(book._year);
誰能幫幫我,我很困惑,謝謝
根據https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties,您的基礎屬性不可writable
下面的作品:
var book = {}; Object.defineProperties(book, { _year: { value: 2004, writable: true }, edition: { value: 1, writable: true }, year: { get: function() { return this._year; }, set: function(newValue) { if (newValue > 2004) { this._year = newValue; this.edition += newValue - 2004; } } } }); book.year = 2006; console.log(book.edition); console.log(book._year);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.