设为首页 加入收藏

TOP

前端开发JS——对象与原型(二)
2019-09-23 18:10:57 】 浏览:86
Tags:前端 开发 对象 原型
urable]]: 表示是否通过delete删除属性从而重新定义属性,能否修改属性的特性,或者能否把属性修改为访问 器属性直接定义在对象中,默认为true)
 
[[Enumerable]]: 表示能否通过for-in循环返回属性。(直接定义在对象中,默认为true)
 
[[Wriable]]: 表示能否修改属性的值。(直接定义在对象中,默认为true)
 
[[Value]]: 包含这个属性的数据值 name:jacky
 
          ③对象的属性特性
var obj = {
  name:'zhangsan',
  age:12,
};
console.log(obj); //{name:'zhangsan', age: 12,}
 
读取属性的特性 Object.getOwnPropertyDescriptor();获取指定属性的描述符该方法接受两个参数, 第一个为属性所在的对象,第二个为要读取其描述符的属性名称
 
//获取属性的描述信息
console.log(Object.getOwnPropertyDescriptor(obj, 'name'));   //{vale: 'zhangsan', writerable:true, enumerable:true, configurable: true}
 
要修改属性默认的特性,必须使用ECMAScript5的Object.defineProperty()方法 defineProperty(属性所在的对象,属性的名字,一个描述符对象);
 
//修改属性的描述信息
Object.defineProperty(obj, 'name', {
//设置属性不可枚举
enumerable: false,   ------->console.log(obj);   //{age:12}
//设置属性不可修改
writable:false,             -------->obj.name = 'lisi'; console.log(obj.name); //zhangsan
//设置属性值
value:'terry',              --------->console.log(obj.name);     //terry
//设置属性是否可以删除delete,是否可以重新定义,是否可以配置
configurable:false,         --------->别人就不能改变这个属性
})
 
④访问器属性      没有值,有get方法和set方法, configurable的属性默认为false
var obj = {
  name:'zhangsan',
  age:12,
  _weight:100,
};
Object.defineProperty(obj, 'weight', {
  get:function(){
    return this._weight-5;
  },
  set:function(param){
    this._weight = param
  },
  //enumerable:true,
});
/*Object.defineProperty(obj, '_weight', {
  enumerable:false
});*/
console.log(obj.weight);   //95
obj.weight = 130
console.log(obj.weight);   //125
console.log(obj._weight);   //130
console.log(obj);  //   {name:'zhangsan', age: 12, weight: [Getter|Setter]}
                    console.log(Object.getOwnPropertyDescript(obj, 'weight'));
console.log(Object.getOwnPropertyDescript(obj, '_weight'));
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇CSS函数大全 下一篇li每三个换行

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目