Javascript Object.defineProperty()

來源:互聯網
上載者:User

Javascript Object.defineProperty()
Javascript作為一種語言,有個美譽,開發人員可以重新定義任何事情。雖然這在過去的一些javascript可以,但是ECMAScript5中已經開始得到改變,例如,我們可以使用Object.defineProperty建立一個不能被修改的對象的屬性。本文中我們將講述Object.defineProperty的基本用法。 如果你想在文章開始之前,深入瞭解Object.defineProperty方法,請戳。 一、基本用法假如我想構建一個math.js庫,看下面的執行個體:  var mathObj = {    constants: {        "pi": 3.14    },    areaOfCircle: function(radius) {        return this.constants.pi*radius*radius;    }} 在上例中,如果有人改變pi的值,那麼我們將不會得到正確的計算結果,雖然有很多方法可以解決此問題,但是最簡單的方法是使用pi屬性不可寫。看下面執行個體:  var mathObj = {    constants: {},    areaOfCircle: function(radius) {        return this.constants.pi*radius*radius;    }}  Object.defineProperty(mathObj.constants, "pi", {    value: 3.14,    writable: false}); mathObj.constants.pi = "Benjamin"; //Outputs: 3.14console.log(mathObj.constants.pi);Object.defineProperty(obj, prop, descriptor)方法接收三個參數:需要添加或修改屬性的對象,屬性名稱,屬性描述options。從上例可以看出,當給pi賦值為“Benjamin”時,最後輸出的值還是3.14。 但是如果給math.js使用“use strict",將會報錯,和給undefined賦值一樣:  "use strict";var mathObj = {    constants: {},    areaOfCircle: function(radius) {        return this.constants.pi*radius*radius;    }}  Object.defineProperty(mathObj.constants, "pi", {    value: 3.14,    writable: false}); mathObj.constants.pi = "Benjamin"; //<span style="color: #ff0000;">Outputs: Uncaught TypeError: Cannot assign to read only property 'pi' of #<Object></span> console.log(mathObj.constants.pi);第三個參數的options中,writable預設值為false,所以在上例中可以省略,configurable預設值為false,如果你想使用你的庫的使用者故意重寫pi的值,你可以設定configurable值為true。  Object.defineProperty(principia.constants, "pi", {    value: 3.14,    configurable: true});但是當你使用Object.defineProperty時,也有一種相當大的Hack,即使設定了writable的值,它也不會保持屬性值不變的:  var container = {}; Object.defineProperty(container, "arr", {    writable: false,    value: ["a", "b"]}); container.arr = ["new array"]; // Outputs: ["a", "b"]console.log(container.arr); container.arr.push("new value"); // Outputs: ["a", "b", "new value"]console.log(container.arr);arr數組是不可寫的,所以始終指向同一個數組,但是數組的成員是可以變化的,所以將來可能會增加鎖定數組或者對象來解決此問題。 二、相容性因為Object.defineProperty方法是ES5的一部分,所以在IE9及現代瀏覽器,IE8中只得到了部分實現,盡可以使用在DOM對象上,不幸的是,並沒有IE8相關的shim來相容。但是,如果你不需要處理舊的瀏覽器,defineProperty可能會有你使用的地方。 以上就是對Object.defineProperty方法的描述,文中不妥之處,還望批評指正。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.