zoukankan      html  css  js  c++  java
  • javascript面向对象的程序设计之Object.getOwnPropertyDescriptor()

    Object.getOwnPropertyDescriptor()用于获取给定属性的描述信息,这个描述信息是一个对象.

    如果是访问器属性,则这个对象的属性有configurable,enumerable,get,set.

    如果是数据属性,则这个对象属性有,configurable,enumerable,writable,value.

     1  var human = {
     2         _age:18,//下划线是一种常用的记号.用于只能通过对象方法访问的属性
     3         role:"大人"
     4     };
     5     Object.defineProperty(human, "age", {
     6         get: function () {//放回age属性值
     7             return this._age;
     8         },
     9         set: function (ageValue) {//设置对象中的属性值
    10             if (ageValue >= 18)
    11                 this.role = "大人";            
    12             else
    13                 this.role = "小孩";
    14         }
    15     });
    16     var descriptor = Object.getOwnPropertyDescriptor(human, "_age");//访问数据属性
    17     alert(descriptor.value);//18
    18     alert(descriptor.configurable); //true
    19     alert(typeof descriptor.get);//undefined
    20     descriptor = Object.getOwnPropertyDescriptor(human, "age");//访问访问器属性
    21     alert(descriptor.value);//undefined
    22     alert(descriptor.enumerable);//false
    23     alert(typeof descriptor.get);//function
  • 相关阅读:
    有个名字叫随便乱记——css3
    CSS3伸缩布局
    路政整理
    GIst
    SVN回滚版本
    你需要知道的CSS3 动画技术
    iScroll框架的使用和修改
    CSS3阴影 box-shadow的使用和技巧总结
    Javascript异步编程的4种方法
    zepto学习零碎
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/3591484.html
Copyright © 2011-2022 走看看