物件賦值
一開始學的物件賦值都是使用直接賦值的方式,然而物件有提供 取值
、賦值
的功能,可以做出類似物件內函式的操作呦
1 2 3 4 5 6 7
| var wallet = { total: 100, };
wallet.total = 300;
console.log(wallet.total);
|
get、set
設定物件 get
、set
有兩種方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| var wallet = { total: 100, set save(price) { this.total = this.total + price; }, get save() { return this.total / 2; }, }; console.log(wallet.save);
wallet.save = 300;
console.log(wallet.save); console.log(wallet); console.log(Object.getOwnPropertyDescriptor(wallet, 'save'));
|
此時的 save
是可以被列舉、可被刪除的設定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| var wallet = { total: 100, };
Object.defineProperty(wallet, 'save', { set: function(price) { this.total = this.total + price; }, get: function() { return this.total / 2; }, });
wallet.save = 300;
console.log(wallet.save); console.log(wallet); console.log(Object.getOwnPropertyDescriptor(wallet, 'save'));
|
由於此方法會讓 save
視為原型屬性,所以是不可列舉的、不可被刪除的設定,除非有再額外調整特徵。
修正後:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| var wallet = { total: 100, };
Object.defineProperty(wallet, 'save', { configurable: true, enumerable: true, set: function(price) { this.total = this.total + price; }, get: function() { return this.total / 2; }, });
wallet.save = 300;
console.log(wallet.save); console.log(wallet); console.log(Object.getOwnPropertyDescriptor(wallet, 'save'));
|
陣列也適用
由於陣列也是物件的一種,所以也能使用 get
、set
,但只能用 defineProperty
設定哩
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| var a = [1, 2, 3]; var b = [4, 5, 6]; var c = [7, 8, 9]; Object.defineProperty(a, 'latest', { get: function() { return this[this.length - 1]; }, });
console.log(a.latest); console.log(b.latest);
Object.defineProperty(Array.prototype, 'latest', { get: function() { return this[this.length - 1]; }, });
console.log(c.latest);
|
參考資料
六角學院 - JavaScript 核心篇