晴明的博客园 GitHub      CodePen      CodeWars     

[js] OO(面向对象) 属性

#

  1 <!DOCTYPE html>
  2 <html>
  3 
  4     <head>
  5         <meta charset="UTF-8">
  6         <title></title>
  7     </head>
  8 
  9     <body>
 10         <script>
 11             /*
 12              * 数据属性
 13              */
 14             var person = {};
 15             Object.defineProperty(person, "name", {
 16                 writable: true, //能否修改属性的值
 17                 configurable: true, //能否删除(delete)属性,一旦设置为false,不可更改
 18                 enumberable: true, //能否for-in 循环返回属性
 19                 value: "Nicholas" //设置属性的值,默认 undefined
 20             });
 21             console.log(person.name);
 22             person.name = "Michael";
 23             console.log(person.name);
 24             /*
 25              * 访问器属性
 26              */
 27             var book = {
 28                 _year: 2004,
 29                 edition: 1
 30             };
 31             Object.defineProperty(book, "year", {
 32                 get: function() {
 33                     return this._year;
 34                 }, //读取属性时调用的函数
 35                 set: function(newValue) {
 36                         if (newValue > 2004) {
 37                             this._year = newValue;
 38                             this.edition += newValue - 2004;
 39                         }
 40                     } //写入属性时调用的函数
 41             });
 42             book.year = 2005;
 43             console.log(book._year);
 44             console.log(book.edition); //2
 45             /*
 46              * defineProperties写法
 47              */
 48             var book = {};
 49             Object.defineProperties(book, {
 50                 _year: {
 51                     writable: true, //此时必须指定
 52                     value: 2004
 53                 },
 54                 edition: {
 55                     writable: true, //此时必须指定
 56                     value: 1
 57                 },
 58                 year: {
 59                     get: function() {
 60                         return this._year;
 61                     },
 62                     set: function(newValue) {
 63                         if (newValue > 2004) {
 64                             this._year = newValue;
 65                             this.edition += newValue - 2004;
 66                         }
 67                     }
 68                 }
 69             });
 70             book.year = 2005;
 71             console.log(book._year);
 72             console.log(book.edition); //2
 73             /*
 74              * 读取属性特性
 75              */
 76             var book = {};
 77             Object.defineProperties(book, {
 78                 _year: {
 79                     value: 2004
 80                 },
 81                 edition: {
 82                     value: 1
 83                 },
 84                 year: {
 85                     get: function() {
 86                         return this._year;
 87                     },
 88                     set: function(newValue) {
 89                         if (newValue > 2004) {
 90                             this._year = newValue;
 91                             this.edition += newValue - 2004;
 92                         }
 93                     }
 94                 }
 95             });
 96             var descriptor = Object.getOwnPropertyDescriptor(book, "_year");
 97             console.log(descriptor.value); //2004
 98             console.log(descriptor.configurable); //false,不设置的话默认false
 99             console.log(typeof descriptor.get); //"undefined"
100             var descriptor = Object.getOwnPropertyDescriptor(book, "year");
101             console.log(descriptor.value); //undefined
102             console.log(descriptor.enumerable); //false,不设置的话默认false
103             console.log(typeof descriptor.get); //"function"
104         </script>
105     </body>
106 
107 </html>

 一般来说,对象的属性 用 _xx 形式写。

posted @ 2016-02-21 17:17  晴明桑  阅读(263)  评论(0编辑  收藏  举报