Object defineProperty

Object defineProperty

  • 一、簡介
    • 1. 屬性表
    • 2.互斥性
    • 3. get、set的簡單使用
  • 二、深入
    • 1.定義常量
    • 2. Object.preventExtensions() 禁止對象拓展(不可逆)
    • 3. Object.seal() 密封(不可逆)
    • 4. Object.freeze() 凍結(不可逆)
  • 三、應用

一、簡介

defineProperty可以詳細的配置一個對象的屬性的特性和值
賦值的兩種方式:

var time = {}
// 第一種
time.kind1 = 1;  // var test = {kind1:1}
// 第二種
Object.defineProperty(time,"kind2",{value:2
})
// ================================分割線===================================
// 第一種等價于
Object.defineProperty(time,"kind1",{value:1,writable:true,enumerable:true,configurable:true,// get:undefined,// set:undefined
})
// 第二種等價于
Object.defineProperty(time,"kind2",{value:2,writable:false,enumerable:false,configurable:false,// get:undefined,// set:undefined
})

1. 屬性表

說明defineProperty后的默認值=后的默認值擁有get、set后的默認值
value屬性的默認值undefinedundefinedget的return值(不能配置)
writablevalue是否可被重寫,作用于等于號賦值和defineProperty賦值。configurable為false時,被設為false后就不能在被設為truefalsetruetrue(不能配置)
enumerable是否可枚舉(被循環顯示)。configurable為false時,就不能再被更改false(為false時谷歌控制臺顯示該屬性為淡紫色)truefalse
configurable屬性的配置項是否可再此被定義,或被delete。configurable為false時,就不能再被更改falsetruefalse
get獲取屬性值時調用的方法undefinedundefined-
set設置屬性值時調用的方法undefinedundefined-
*[配置項]: 除value的選項

2.互斥性

數據描述符:value、writable;存取描述符:get、set

value、writable 不能與get、set共存:
當設置了value,就不需要再從get獲取值(該從value取值還是從get取值);當設置了writable為false時,set將沒有意義(不允許再修改)


get、set不能與value、writable共存:
當設置了get、set,就不需要從value獲取、更改值(有了自定義的存取值方法);當設置set時,writable必須為true(必須允許修改)

3. get、set的簡單使用

var time = {__date: new Date(),__lastEditTime: "",__lastReadTime: "",__remark: "",__copyright: " (GeniusXYT)",__getNow: function () {return new Date().toLocaleString();}
};
Object.defineProperty(time, "createTime", {get() {return this.__date.toLocaleString();},set(val) {console.warn(`the obj property(now) do not be set ${val}.`);},enumerable: true
});
// 同時定義多個屬性
Object.defineProperties(time, {lastReadTime: {get() {return this.__lastReadTime;},enumerable: true},lastEditTime: {get() {return this.__lastEditTime;},enumerable: true},remark: {get() {this.__lastReadTime = this.__getNow();return this.__remark ? this.__remark + this.__copyright : "";},set(val) {this.__lastEditTime = this.__getNow();this.__remark = val;},enumerable: true,configurable: true // 可重寫 enumerable、configurable、get和set}
});
//  將帶有__命名的變量(默認代表私有變量)隱藏(不可枚舉)
(function hidePrivateVarible(obj) {for (let k of Object.keys(obj)) {if (k.slice(0, 2) === "__")Object.defineProperty(obj, k, { enumerable: false, configurable: false });}
})(time);

二、深入

1.定義常量

定義后屬性值不能被更改(同const)

var time={};
// 方法一
Object.defineProperty(time,"author",{value:"GeniusXYT",writable:false,enumerable:true,configurable:false
})
// 方法二
Object.defineProperty(time,"author2",{enumerable:true,configurable:false,get:function (){return "GeniusXYT"},set:function(val){console.error("the obj time do not set property 'author',value '"+val+"'.")}
})
time.author="new name"
console.log(time.author)

2. Object.preventExtensions() 禁止對象拓展(不可逆)

禁止一個對象添加新屬性并且保留已有屬性
這樣對象就不會再有新屬性

var time = {};
time.prop = "prop";
console.log(Object.isExtensible(time));
Object.preventExtensions(time);
time.newProperty = "new";
console.log(time); // newProperty undefined
console.log(Object.isExtensible(time));

3. Object.seal() 密封(不可逆)

在對象上調用object.preventExtensions(),并把所有現有屬性標記為configurable:false
這樣對象就不會再有新屬性,無法再配置特性

var time = {};
Object.defineProperty(time,"prop",{value:"prop(i can configurable)",writable:true,enumerable:true,configurable:true,
});
console.log(Object.isExtensible(time));
Object.seal(time);
time.newProperty = "new";
console.log(time); // newProperty undefined
console.log(Object.isExtensible(time));
Object.defineProperty(time,"prop",{writable:false}); // configurable為false時,可以將writable設為false;
time.prop = "new prop";
console.log(time.prop);
Object.defineProperty(time,"prop",{writable:true}); // 報錯 設為false后將不能再設為true
Object.defineProperty(time,"prop",{enumerable:false}); // 報錯 此時configurable已經被seal設置為false
Object.defineProperty(time,"prop",{configurable:true}); // 報錯 configurable為false時,就不能再被更改

4. Object.freeze() 凍結(不可逆)

在一個對象上調用Object.seal(),并把所有現有屬性標記為writable: false
這樣對象就不會再有新屬性,無法再配置特性,無法再設置值,對象就永遠是那個對象了

var time = {};
Object.defineProperty(time,"prop",{value:"prop(i can configurable)",writable:true,enumerable:true,configurable:true,
});
console.log(Object.isExtensible(time));
Object.freeze(time);
time.newProperty = "new";
console.log(time); // newProperty undefined
console.log(Object.isExtensible(time));
Object.defineProperty(time,"prop",{writable:true}); // 報錯 設為false后將不能再設為true
Object.defineProperty(time,"prop",{enumerable:false}); // 報錯 此時configurable已經被seal設置為false
Object.defineProperty(time,"prop",{configurable:true}); // 報錯 configurable為false時,就不能再被更改
time.prop = "new prop"; // 無效 此時的writable: false
console.log(time.prop);

三、應用

vue雙向綁定……

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278571.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278571.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278571.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

jhope代碼分析以及網站結構

如下圖所示,為Extjs部分代碼提供的網頁結構:網站看上去本來是這樣的前端采用ExtJS,與后臺的SpringMVCSpringHibernate進行數據交互。之前分析過登錄的過程,不贅述在loginController處理登錄返回結果的最后,如下語句也就…

Ubuntu下Authentication token manipulation error或者Authentication Failure解決辦法

在Ubuntu18.04使用以下命令出現以下錯誤: 用passwd為新建用戶或者root添加密碼:Authentication token manipulation error 切換root用戶出現Authentication Failure. 網上出現了大致兩種方法: 第一種:用戶文件和密碼文件被保護,用chattr命令移除保護即可…

初學者:如何使用虛擬PC將Windows 7安裝到虛擬機

Continuing in our series covering how to use Virtual PC, this week we’ll be showing you how to install Windows 7 into a virtual machine. It’s a very simple process, but here’s the step-by-step guide for beginners. 繼續我們的系列文章,介紹如何使…

arcgis本地服務快速遷移到新機

情景 在本機或服務器發布了幾十、幾百個gis服務,當換電腦或者換服務器時不可能挨個找源文件重新發布服務,于是就想著既然是本地文件,一定可以拷貝過去的,經過一番搜索,結果如下: 方案一、遷移至新站點 新機站點創建…

js中 給json對象添加屬性和json數組添加元素

json對象: 比如現在有一個json對象為jsonObj,需要給這個對象添加新的屬性newParam,同時給newParam賦值為pre。做法如下: var jsonObj{param1:22,param2 :33}; 現在給jsonObj添加一個新的屬性newParam jsonObj.newParam pre; 新的…

zabbix中php信息缺失之后的安裝

安裝php下enable bcmath和gettext (在安裝php時可以添加 --enable-bcmath --enable-gettext)1,bcmath安裝方法bcmath這個擴展在php源安裝包壓縮包中都是有的,需要重新編譯一下才能夠支持;cd php-5.2.7/ext/bcmath(源…

極客大佬用什么電腦_極客特惠:筆記本電腦,高清電視和免費應用

極客大佬用什么電腦If you love new gear but not high prices then we’ve got some deals for you; grab some deeply discounted laptops, monitors and HDTVs, and free mobile apps in this week’s Geek Deals roundup. 如果您喜歡新設備,但不喜歡高價&#x…

Linux內核 TCP/IP、Socket參數調優

詳見http://blog.csdn.net/u010009038/article/details/51917460轉載于:https://blog.51cto.com/jack88/2063979

ppt插入html(用office而不是wps)

最近新get到的技能,在ppt里面插入html!注意要用 Microsoft Office PowerPoint 才行,而不是wps,一定要先安裝Microsoft Office PowerPoint再執行以下操作。 1、修改注冊表的值,才能在PowerPoint中插入 Microsoft Web B…

如何使用SkyDrive的25 GB作為映射驅動器以方便訪問

SkyDrive is an online storage system included in Windows Live, which gives you 25 GB of space that you can sync to your desktop. Here’s how to connect it to your Windows 7 computer as a mapped drive. SkyDrive是Windows Live中包含的一個在線存儲系統&#xff…

SpringBoot+Mybatis 框架之 @SelectProvider注解方式搭建

之前搭建了Select標簽來做SringBootMybatis的集成。這次使用SelectProvider標簽的方式搭建一次。 一、搭建SpringBoot的項目 https://start.spring.io/自己配置SpringBoot的項目,點擊“Generate Project”按鈕就可以下載下來一個配置好的SpringBoot項目。 二、項目結…

程鑫峰:1.23日央行推行負利率政策,倫敦金后市行情解析

程鑫峰:1.23日央行推行負利率政策,倫敦金后市行情解析 QQ截圖20180123153028.png ??盡管美國政府關門鬧劇剛剛結束,但交易員、投資者和策略師對于美元的前景依然不太樂觀。美國貨幣政策對美元的影響力減弱可能是全球通貨再膨脹交易的另一個…

從購買域名到nginx,flask搭建自己的網站

搭建一個只屬于自己的網站? 一、注冊域名(可選*) 1.注冊阿里云賬號 網址:登錄(注冊) 2.購買域名:阿里云域名注冊 有一元域名、免費域名等。 購買過程中需要創建信息模板(必須完成郵箱真實…

alexa語音實現_如何通過語音刪除Alexa錄音

alexa語音實現Amazon亞馬孫Amazon is rolling out new privacy features today for Alexa. In addition to an educational “privacy hub,” the company lets you delete your stored recordings by voice. But it’s off by default; you’ll need to flip a switch. 亞馬遜…

linux如何查看所有的用戶(user)、用戶組(group)、密碼(password/passwd)

linux如何查看所有的用戶和組信息_百度經驗https://jingyan.baidu.com/article/a681b0de159b093b184346a7.html linux添加用戶、用戶組、密碼_百度經驗https://jingyan.baidu.com/article/335530da8b7e0419cb41c3e5.html 給用戶開通sudo權限 xxx is not in the sudoers file.Th…

angular之兩種路由

安裝angular npm install -g angular/cli ng new myapp ng g component componentName 自帶路由 引入&#xff1a;angular-route.js <div ng-controllerctr1><a href#home>首頁</a> <a href#mine>我的</a> <div ng-view></div><d…

用scrapy框架寫爬蟲

爬蟲可以發送給引擎的兩種請求&#xff1a; # 1、url&#xff1a;# &#xff08;爬蟲&#xff09;yield scrapy.Request -> 引擎 -> 調度器&#xff08;發送給調度器入隊&#xff09; -> 引擎&#xff08;調度器出隊請求于引擎&#xff09;# -> 下載器&#xff08;…

audacity_如何在Audacity中快速編輯多個文件

audacityGot a bunch of files that need to be edited the same way? You can automate the process to save time and effort using Audacity’s Chain feature and modify tons of files at the same time. 有一堆需要以相同方式編輯的文件&#xff1f; 您可以使用Audacity…

通過api管理grafana

1. 生成api key 參考&#xff1a; http://docs.grafana.org/http_api/auth/ 2.點擊添加后&#xff0c;生成了個獲取一個deshboards的api樣例 3.放到linux上運行測試&#xff0c;結果成功返回。 4. 有些api并不支持使用api key 來連接&#xff0c;如下圖中的搜索用戶接口&#x…

NFS服務的配置過程

NFS服務的配置過程服務端:1)安裝nfs和rcp服務yum install nfs-utils rpcbind -y 因為NFS支持的功能多,不同的功能會使用不同的程序來啟動每啟動一個功能就會啟動一些端口來傳輸數據,默認NFS讀完啟動會產生多個進程,多個端口號信息,會隨機使用未被使用的端口重啟又會變化,所以…