对象的数据属性

Configurable,Enumerable,Writable,Value

var person = {}
Object.defineProperty(person,'name',{
configurable:false,//能否使用delete、能否需改属性特性、或能否修改访问器属性、,false为不可重新定义,默认值为true
enumerable:false,//对象属性是否可通过for-in循环,flase为不可循环,默认值为true
writable:false,//对象属性是否可修改,flase为不可修改,默认值为true
value:'xiaoming' //对象属性的默认值,默认值为undefined
}); //value
console.log(person);//xiaoming,默认value //writable
person.name="qiang";
console.log(person);//xiaoming,不可修改value //enumerable
for(var i in person){
console.log(person[i]) //无结果,不可循环
} //configurable
delete person.name
console.log(person.name)//xiaoming,不可删除 Object.defineProperty(person,'name',{
configurable:true //不可修改,将抛出错误
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

访问器属性

getter,setter

var book = {
_year: 2004,//属性前面加_,代表属性只能通过对象方法访问
edition: 0
}
Object.defineProperty(book,'year',{
get: function(){
return this._year;
},
set: function(newValue){
if(newValue > 2004){
this._year = newValue;
this.edition += newValue - 2004
}
}
});
console.log(book.year)//2004
book.year = 2006;
console.log(book.year)//2006
console.log(book.edition)//2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

定义多个属性

Object.defineProperties

var book = {};
Object.defineProperties(book, {
_year: {
value:2004,
writable:true
},
edition: {
value: 0,
writable:true
},
year: {
get: function() {
return this._year;
},
set: function(newValue) {
if (newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004
}
}
}
});
console.log(book.year) //2004
book.year = 2006;
console.log(book.year) //2006
console.log(book.edition) //2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

读取属性

var book = {};
Object.defineProperties(book, {
_year: {
value:2004,
writable:true
},
edition: {
value: 0,
writable:true
},
year: {
get: function() {
return this._year;
},
set: function(newValue) {
if (newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004
}
}
}
});
console.log(book.year) //2004
book.year = 2006;
console.log(book.year) //2006
console.log(book.edition) //2
//读取属性
var descriptor__year = Object.getOwnPropertyDescriptor(book,'_year');
var descriptor_year = Object.getOwnPropertyDescriptor(book,'year');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

console.log(descriptor__year ) 

console.log(descriptor_year ) 

版权声明:如需转载请注明出处。 http://blog.csdn.net/qq_17335153/article/details/52584727

js Object的属性 Configurable,Enumerable,Writable,Value,Getter,Setter的更多相关文章

  1. js - object的属性操作

    视频学习地址: http://www.imooc.com/video/6002 原文PPT下载地址: http://img.mukewang.com/down/54c5ec1a000141f10000 ...

  2. js object 对象 属性和方法的使用

    //object 对象 属性和方法的使用 var person = new Object(); person.name="张海"; person.age="; perso ...

  3. js Object.defineProperty 使用

    语法 Object.defineProperty(obj, prop, descriptor) 参数说明: obj:必需.目标对象 prop:必需.需定义或修改的属性的名字descriptor:必需. ...

  4. js动态参数作为Object的属性取值

    js动态参数作为Object的属性取值var myObj = {"a":1,"b":2};var a = 'a';myObj[a] 就可以获取到 属性a的值了

  5. js object(对象)

    http://www.cnblogs.com/pingchuanxin/p/5773326.html Object(对象)是在所有的编程语言中都十分重要的一个概念,对于事物我们可以把他们看作是一个对象 ...

  6. js object 常用方法总结

    Object.assign(target,source1,source2,...) 该方法主要用于对象的合并,将源对象source的所有可枚举属性合并到目标对象target上,此方法只拷贝源对象的自身 ...

  7. JS Object.defineProperties()方法

    JS Object.defineProperties()方法 描述: Object.defineProperties()方法为目标对象同时配置多个属性. 语法: Object.defineProper ...

  8. JS object(对象)的学习汇总

    Object(对象)是在所有的编程语言中都十分重要的一个概念,对于事物我们可以把他们看作是一个对象,而每一个事物都有自己的表示的属性和对于某一信息作出的相应的操作.而这些东西就变成了事物的属性和方法. ...

  9. 这次彻底理解了Object这个属性

    1.实例化Object对象 实例化Object对象的方式有两种:使用Object构造器和使用对象的字面量.例如: var person1 = { name: '李四' }; var person2 = ...

随机推荐

  1. 'No application found. Either work inside a view function or push'

    问题: 说是create_all()的地方有问题,莫名其妙. 后来经过查资料,找出解决方法.附上代码如下:

  2. Spring Cloud第十二篇 | 消息总线Bus

    ​ ​本文是Spring Cloud专栏的第十二篇文章,了解前十一篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring ...

  3. 在浏览器下载pdf,或者txt文档是会直接打开

    window.location.href = url会直接打开,解释大概是因为浏览器自身可以解析.pdf或者txt.解决方法如下: 本来就要用a标签里面加上download属性的,结果发现不行,就算了 ...

  4. 批量下载文件php

    做了个照片墙,要提供批量下载照片的功能,如果你会文件下载,那批量也是小菜一碟,就是把文件打包压缩为  zip 文件再下载,而php的内置类ZipArchive()让你很容易实现. 首先,配置php.i ...

  5. Comet OJ - Contest #6 C 一道树题 数学 + 推导

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...

  6. 好用的jQuery分页插件

    插件源代码: (function ($) { $.fn.extend({ smartpaginator: function (options) { var settings = $.extend({ ...

  7. 基于点云的3ds Max快速精细三维建模方法及系统的制作方法 插件开发

                                 基于点云的3ds Max快速精细三维建模方法及系统的制作方法[技术领域][0001]本发明涉及数字城市三维建模领域,尤其涉及一种基于点云的3d ...

  8. Solr单机环境搭建及部署

    一.定义 官网的定义: Solr是基于Lucene构建的流行,快速,开放源代码的企业搜索平台.它具有高度的可靠性,可伸缩性和容错能力,可提供分布式索引,复制和负载平衡查询,自动故障转移和恢复,集中式配 ...

  9. canvas 方块旋转案例

    <!doctype html><html><head> <meta charset="UTF-8"> <meta name=& ...

  10. leetcode-mid-Linked list- 103. Binary Tree Zigzag Level Order Traversal

    mycode 99.24% # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x) ...