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.mzph.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读完启动会产生多个进程,多个端口号信息,会随机使用未被使用的端口重启又会变化,所以…