我用一個使用Object.defineProperty
的getter定義了一個屬性,我希望能夠覆蓋它而不需要使用defineProperty
來防止將要消耗它的同事的混淆,但可能不會知道這不是一個「正常」的財產。我試過writeable
和configurable
選項無濟於事。重寫沒有defineProperty的對象獲取器
var fixture = {
name: 'foo',
color: 'green'
};
Object.defineProperty(fixture, 'email', {
get: function() {
return 'test+' + Date.now() + '@gmail.com';
},
configurable: true,
writeable: true
});
console.log(fixture.email); // test+<date>@gmail.com
fixture.email = 'bob'; // consumer attempts to overwrite
console.log(fixture.email); // test+<date>@gmail.com T_T
// this _will_ modify the property
Object.defineProperty(fixture, 'email', {
value: 'overwritten'
});
console.log(fixture.email);