var person = { _name: '', get name() { return this._name }, set name(n) { this._name = n } } // 測試 person.name // 輸出 --> '' person.name = 'Zhangsan' person.name // 輸出 --> Zhangsan
var person = function() { var _name = ' '; var obj = {}; Object.defineProperty(obj, 'name', { configurable: true, enumerable: true, get: function() { return _name; }, set: function(n) { _name = n; } }) return obj; }(); person.name = "Zhangsan"; person.name // 輸出 --> Zhangsan