2015-04-02 42 views
0

我用一個使用Object.defineProperty的getter定義了一個屬性,我希望能夠覆蓋它而不需要使用defineProperty來防止將要消耗它的同事的混淆,但可能不會知道這不是一個「正常」的財產。我試過writeableconfigurable選項無濟於事。重寫沒有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);

回答

3

勢點二傳手我已經試過了可寫和可配置選項都無濟於事。

writable只適用於數據屬性,即具有value不是吸氣和吸氣裝置的數據屬性。

configurable是隻允許刪除和重新定義,而不是使屬性可設置。

如果你想要fixture.email = 'bob';不要拋出一個錯誤,你必須在你的屬性對象上提供一個set方法。現在該方法可以:

  • 忽略新值
  • 存儲新的值,例如在不同的屬性或關閉變量,使吸氣劑可以產生它後續的訪問(@Icepickle在他回答的這一些例子)
  • 存取屬性轉換回「正常」的一個

最後可能是最簡單也是最有收穫的選擇:

var fixture = { 
    name: 'foo', 
    color: 'green' 
}; 

Object.defineProperty(fixture, 'email', { 
    get: function() { 
    return 'test+' + Date.now() + '@gmail.com'; 
    }, 
    set: function(val) { 
    Object.defineProperty(this, 'email', { 
     value: val, 
     writable: true 
    }); 
    }, 
    configurable: true, 
    enumerable: true 
}); 

console.log(fixture.email); // test+<date>@gmail.com 
fixture.email = 'bob'; // consumer attempts to overwrite 
console.log(fixture.email); // bob 
1

雖然,我不會親自使用它,我想你可以去解決問題,只有讓它當值特定於您的期望,如果它不是一個設置正確的值,拋出某種錯誤?

不利的一面當然是,一旦他們看到代碼放在代碼中就像這樣,他們也可以這樣做,但我想他們已經不那麼困惑了;)我實際上更喜歡第二種選擇我在通過更新裏面的「DataContainer」

var data = { 
 
    foo: 'foo', 
 
    bar: 'bar' 
 
}; 
 

 
Object.defineProperty(data, 'email', { 
 
    get: function() { 
 
    if (typeof this._props === 'undefined') { 
 
     this._props = {}; 
 
    } 
 
    return this._props.email; 
 
    }, 
 
    set: function(val) { 
 
    if (!val || !val.private) { 
 
     throw 'no access exception'; 
 
    } 
 
    if (this.email === val) { 
 
     return; 
 
    } 
 
    this._props.email = val.email; 
 
    }, 
 
    configurable: false 
 
}); 
 

 
console.log(data.email); 
 
data.email = { 
 
    private: 1, 
 
    email: '[email protected]' 
 
}; 
 
console.log(data.email); 
 
try { 
 
    data.email = 'not allowed to set'; 
 
} finally { 
 
    console.log(data.email); 
 
}

的props.email屬性可能的另一種方式,僅僅提供了確實的設置本身,或者通過額外的setEmail方法在內部底部呈現只需將邏輯構建到類構造器中,然後添加一個在一個函數(或內部通過可變)

function DataContainer(options) { 
 

 
    var createProp = function(obj, propertyName, propertyHolder, isReadOnly) { 
 
     Object.defineProperty(obj, propertyName, { 
 
     get: function() { 
 
      return propertyHolder[propertyName]; 
 
     }, 
 
     set: function(val) { 
 
      if (isReadOnly) { 
 
      return; 
 
      } 
 
      propertyHolder[propertyName] = val; 
 
     }, 
 
     configurable: false 
 
     }); 
 
    
 
     if (isReadOnly) { 
 
     obj['set' + propertyName[0].toUpperCase() + propertyName.substr(1)] = function(val) { 
 
      //internal setter 
 
      propertyHolder[propertyName] = val; 
 
     }; 
 
     } 
 
    }, 
 
    prop = {}, 
 
    fieldProp, fieldVal, ro; 
 

 
    if (options && options.fields) { 
 
    for (fieldProp in options.fields) { 
 
     if (options.fields.hasOwnProperty(fieldProp)) { 
 
     fieldVal = options.fields[fieldProp]; 
 
     ro = false; 
 
     if (typeof fieldVal === 'object' && fieldVal.value) { 
 
      ro = fieldVal.readOnly || false; 
 
      prop[fieldProp] = fieldVal.value; 
 
     } else { 
 
      prop[fieldProp] = fieldVal; 
 
     } 
 
     createProp(this, fieldProp, prop, ro); 
 
     } 
 
    } 
 
    } 
 
} 
 

 
var data = new DataContainer({ 
 
    fields: { 
 
    foo: 'foo', 
 
    bar: 'bar', 
 
    email: { 
 
     value: '[email protected]', 
 
     readOnly: true 
 
    } 
 
    } 
 
}); 
 

 
console.log(data); 
 

 
data.email = '[email protected]'; 
 
console.log(data.email); 
 
data.setEmail('[email protected]'); 
 
console.log(data.email);

相關問題