Object.defineProperty和Object.defineProperties
'use strict';
(function(){
if(!Object.defineProperty){
console.log('浏览器不支持defineProperty');
return;
} var person = {}; //向person添加name属性和它的访问器
Object.defineProperty(person, 'name', {
set : function(n){
console.log('set 访问器');
this.nameValue = n; //一个新的属性,如果同样是name的话,就死循环了。
},
get : function(){
console.log('get 访问器');
return 'my name is ' + this.nameValue;
},
//value : 'jeck', //如果在这里有value或writable特性,就会报错:Uncaught TypeError: Invalid property. A property cannot both have accessors and be writable or have a value。
enumerable: true,
configurable: true
});
person.name = "haha"; //触发set访问器
console.log(person.name); //触发get访问器 output: my name is haha
console.log(person.nameValue); //output: haha //向person添加id数据属性。
Object.defineProperty(person, 'id', {
value : '1234567890',
writable : false,
enumerable : false,
configurable : false
});
console.log(person.id); //output: 1234567890
//person.id = '0987654321'; //报错,因为id是只读的,不可写。 //列出对象的属性
var propertyNames = Object.getOwnPropertyNames(person);
for(var i = 0, len = propertyNames.length; i < len; i++){
var name = propertyNames[i]
console.log(name + ': ' + person[name]);
}
/*output:
name: my name is haha
nameValue: haha
id: 1234567890*/ //列出某一属性的所有特性。
var descriptor = Object.getOwnPropertyDescriptor(person, 'id');
descriptor.writable = true;//这里将原来的writable特性改为了true。
for(var prop in descriptor){
console.log(prop + ': ' + descriptor[prop]);
}
/*output:
value: 1234567890
writable: true
enumerable: false
configurable: false*/ //defineProperty的复数版:
Object.defineProperties(person, {
sex : {
value : 'boy',
writable : true,
enumerable : false,
configurable : true
},
age : {
set : function(x){
this.ageValue = x;
},
get : function(){
return this.ageValue;
}
}
}); person.sex = 'girl'; //变性成功!
person.age = 23; console.log('sex: ' + person.sex);
console.log('age: ' + person.age);
/*output:
sex: girl
age: 23*/
})();
Object.defineProperty和Object.defineProperties的更多相关文章
- 对象是否拥有某个属性,in和for in以及object.hasOwnProperty('×××')的异同,以及Object.defineProperty(),Object.keys(),Object.getOwnPropertyNames()的用法
1.在某个对象是否拥有某个属性,判断的方法有很多,常用的方法就是object.hasOwnProperty('×××'),这个方法是不包括对象原型链上的方法的,举个例子: var obj = { na ...
- Object.defineProperty和Object.freeze、Object.seal
目录 一 Object.defineProperty 1.1 用法 1.2 数据描述 1.2.1 value 1.2.2 writable 1.2.3 enumerable 1.2.4 configu ...
- MVVM双向绑定实现之Object.defineProperty
随着web应用的发展,直接操作dom的应用已渐行渐远,取而代之的是时下越来越流行的MVVM框架,dom操作几乎绝迹,这里面自然是框架底层封装的结果.MVVM框架的双向数据绑定使开发效率大大提高:然后在 ...
- 关于Object.defineProperty 的基础知识
Object.defineProperty 这个方法大家耳熟能详,可以对 对象的属性进行添加或修改的操作.即可以进行 数据劫持 .vue就是通过这个方法来劫持数据的. 平时我们创建对象的时候,一般通 ...
- Object.defineProperty实现数据绑定
1.Object.defineProperty方法 Object.defineProperty(obj, prop, descriptor); (1)参数: obj:目标对象 prop:需要定义的属 ...
- vue实现双向数据绑定之Object.defineProperty()篇
前言 vue.js中使用ES5的Object.defineProperty()实现数据的双向绑定 Object.defineProperty()原理 Object.defineProperty()可以 ...
- JS属性描述符之Object.defineProperty()定义对象属性特性
一.Object.defineProperty的作用 用来给对象新增属性,和修改对象中的属性. 二.JS对象中的描述符 js对象中两种属性描述符:数据描述符和存取描述符(访问描述符). 注意事项: 1 ...
- Object.defineProperty和proxy
Object.defineProperty问题 Object.defineProperty() 无法监控到数组下标的变化.vue只能通过以下几种方法来监听 pop() shift() unshift( ...
- js中的Object.defineProperty()和defineProperties()详解
ECMAS-262第5版在定义只有内部采用的特性时,提供了描述了属性特征的几种属性.ECMAScript对象中目前存在的属性描述符主要有两种,数据描述符(数据属性)和存取描述符(访问器属性),数据描述 ...
随机推荐
- Redis单机主从高可用性优化
版权声明:本文由陈龙原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/127 来源:腾云阁 https://www.qclou ...
- 解决Chrome关联Html文件图标显示为空白
用记事本保存为ChromeHTML.reg Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\CLSID\{42042206-2D85-1 ...
- Linux显示不了中文
原文:https://www.cnblogs.com/hooly/p/8615384.html 版权所有:归属原文作者!!! 查看当前系统默认采用的字符集: # locale 在RedHat/C ...
- 【BZOJ1879】[Sdoi2009]Bill的挑战 状压DP
[BZOJ1879][Sdoi2009]Bill的挑战 Description Input 本题包含多组数据. 第一行:一个整数T,表示数据的个数. 对于每组数据: 第一行:两个整数,N和K(含 ...
- java jar命令及补丁方法
用法: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...选项: -c 创建新档案 -t ...
- 【转】jQuery.ajax向后台传递数组问题
$.ajax({ url: "/xxx", type: "GET", data: { "boxIds": boxIds, "box ...
- php基础:面向对象
一.public.private.protected访问修饰符 public:任何都可以访问(本类.子类.外部都可以访问) protected:本类.子类都可以访问(本类.子类均可访问) privat ...
- R-CNN论文详解 - CSDN博客
废话不多说,上车吧,少年 paper链接:Rich feature hierarchies for accurate object detection and semantic segmentatio ...
- 动态修改Python类和实例的方法(转)
相信很多朋友在编程的时候都会想修改一下已经写好的程序行为代码,而最常见的方式就是通过子类来重写父类的一些不满足需求的方法.比如说下面这个例子. class Dog: def bark(self): p ...
- MySQL的知识海洋
第一篇:初识数据库 第二篇:库操作 第三篇:表操作 第四篇:数据操作 第五篇:视图.触发器.存储过程.函数.事物与数据库锁 第六篇:索引原理与慢查询优化 第七篇:pymysql(用python连接以及 ...