大家,我最近读了Professional.JavaScript.for.Web.Developers.3rd.Edition。这是我从中学到的代码。但是,输出与我读过的书有所不同。当我运行下面的代码时,ref.child("Enterpriser Listings").child("Sell Old Stuff - Listings").observe(.value, with: { (snapshot:FIRDataSnapshot) in
var users = [User]()
for child in snapshot.children {
print("\((sweet as! FIRDataSnapshot).value)")
if let dictionary = child.value as? [String : AnyObject] {
let user = User()
user.setValuesForKeys(dictionary)
users.append(user)
}
}
self.userList = users
self.tableView.reloadData()
})
,book.edition is 1
是2004而book._year
是2004.会发生什么?我的代码出了什么问题?
book.year
答案 0 :(得分:3)
对象的属性_year
和edition
应定义为可写。否则,在年度制定者中重新定义它们是没用的。
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;
}
}
}
});
console.log(book.edition);
console.log(book.year);
book.year=2005;
console.log(book.edition);
console.log(book.year);