javascript - 關(guān)于defineProperty的value
問題描述
> var a = {}> Object.defineProperty(a, 'b', {... value: 110}){}> a{}> a.b110> a.b = 555555> a.b110> var aa = {b: 1}undefined> Object.defineProperty(aa, 'b', {... value: 119}){ b: 119 }> aa.b119> aa.b = 11> aa.b1
為什么aa.b可更改,a.b不能?
問題解答
回答1:屬性描述符有三個(gè)屬性分別是 configurable enumerable writable,默認(rèn)值均為 false而使用對(duì)象字面量定義的屬性描述符均為 true,可以通過 Object.getOwnPropertyDescriptor(a, ’b’) 及 Object.getOwnPropertyDescriptor(aa, ’b’) 來獲取兩個(gè)屬性的描述符。
所以 a.b 不可修改,aa.b 可以修改。
回答2:因?yàn)槟J(rèn)情況下,通過Object.defineProperty()定義的屬性的屬性值均為false是不可寫的.
a = {}Object.getOwnPropertyDescriptor(a, ’b’)// > undefinedObject.defineProperty(a, 'b', {value: 119})Object.getOwnPropertyDescriptor(a, ’b’)// > Object {value: 119, writable: false, enumerable: false, configurable: false}
而通過對(duì)象字面量定義的屬性默認(rèn)是可寫的,調(diào)用`
aa = { b: 1 }Object.getOwnPropertyDescriptor(aa, ’b’)// > Object {value: 1, writable: true, enumerable: true, configurable: true}
所以aa的b屬性是可寫的. 所以O(shè)bject.defineProperty()并不改變屬性的屬性值.所以aa的b的值會(huì)改變.
相關(guān)文章:
1. python 利用subprocess庫(kù)調(diào)用mplayer時(shí)發(fā)生錯(cuò)誤2. python - pycharm 自動(dòng)刪除行尾空格3. python - Pycharm的Debug用不了4. python文檔怎么查看?5. datetime - Python如何獲取當(dāng)前時(shí)間6. javascript - 關(guān)于apply()與call()的問題7. html - eclipse 標(biāo)簽錯(cuò)誤8. 請(qǐng)問PHPstudy中的數(shù)據(jù)庫(kù)如何創(chuàng)建索引9. 安全性測(cè)試 - nodejs中如何防m(xù)ySQL注入10. javascript - nginx反向代理靜態(tài)資源403錯(cuò)誤?
