ECMAScript5之Object学习笔记(一)

随着IE的逐步追赶,目前到IE11已经能够很好的支持ECMAScript5标准了,其他的现代浏览器像firefox,chrome,opera就更不用说了。

再加上nodejs使得javascript在后台开发中得到施展的舞台,这很自然的激发了我对ECMAScript5相关的特性的求知欲望。

以此展开,写一个ECMAScript5新特性的学习笔记。

先来看看Object

  • Object.create(proto[, propertiesObject])

    create方法通过指定的原型对象(prototype object)和属性(properties)来创建一个新的对象。

    proto:即为新创建对象的prototype

    propertiesObject:带属性描述的属性对象(姑且这么翻译)

    create方法能够让我们方便的通过prototype原型链来实现“继承”:

     1     // super class
     2     var Human = function(cfg) {
     3         this.gender = cfg.gender;
     4         this.name = cfg.name;
     5     };
     6 
     7     Human.prototype.sayHello = function() {
     8         console.log('Hello, I am ' + this.name);
     9     };
    10 
    11     // sub class
    12     var Citizen = function(cfg) {
    13         this.country = cfg.country; 
    14         // call super constructor
    15         Human.call(this, cfg);
    16     };
    17 
    18     // prototype chain inherit 
    19     Citizen.prototype = Object.create(Human.prototype);
    20     // override constructor
    21     Citizen.prototype.constructor = Citizen;
    22 
    23     // overwrite super class method
    24     Citizen.prototype.sayHello = function() {
    25         console.log('Hello, I am ' + this.name + ' from ' + this.country);
    26     };
    27 
    28     var h = new Human({
    29         gender: 'female',
    30         name: 'lucy'
    31     });
    32     h.sayHello();
    33     console.log( h instanceof Human );
    34 
    35     var c = new Citizen({
    36         gender: 'male',
    37         name: 'Andrew',
    38         country: 'USA'
    39     });
    40     c.sayHello();
    41     console.log( c instanceof Citizen);
  • Object.defineProperty(obj, prop, descriptor)

    defineProperty方法直接在一个对象上定义一个新属性,或者更改一个已存在的属性,返回对象本身。

    obj:对象

    prop: 属性名

    descriptor: 属性描述对象

    用code来做直观说明:

      1 /*
      2     enumerable    是否可列举    默认:false
      3     writable    是否可写    默认:false
      4     configurable是否可配置    默认:false
      5     value    默认:undefined
      6     
      7     访问器
      8     get 默认:undefined  
      9     set    默认:undefined
     10     */
     11     var o = {};
     12 
     13     // enumerable 是否可列举
     14     Object.defineProperty(o, 'a', {
     15         value: 1,
     16         enumerable: false
     17     });
     18 
     19     Object.defineProperty(o, 'b', {
     20         value: 2,
     21         enumerable: true
     22     });
     23 
     24     Object.defineProperty(o, 'c', {
     25         value: 3
     26         // enumerable默认false
     27     }); 
     28 
     29     o.d = 4;
     30 
     31     for(var prop in o) {
     32         // 输出b、d
     33         console.log(prop);  
     34     }
     35 
     36     // writable 是否可写(更改值)
     37     // 这个特性在定义常量时比较有用
     38     Object.defineProperty(o, 'e', {
     39         value: 10,
     40         writable: false
     41     });
     42 
     43     console.log(o.e); // 10
     44     o.e = 15;    // 一般没错误抛出,若是在strict mode(严格模式下)会抛出错误        
     45     console.log(o.e); // 10
     46 
     47     // configurable 是否可配置
     48     Object.defineProperty(o, 'f', {
     49         configurable: true,
     50         get: function() { return 10; }
     51     });
     52 
     53     // 如果configurable为false,那么下面这些redefine(重定义)操作都会报TypeError
     54     // 如果configurable为true,那么下面这些操作均能成功,delete操作也能删除e属性
     55     Object.defineProperty(o, 'f', { configurable: true });    // TypeError
     56     Object.defineProperty(o, 'f', { enumerable: true });    // TypeError
     57     Object.defineProperty(o, 'f', { set: function() {} });    // TypeError
     58     Object.defineProperty(o, 'f', { get: function() {return 10;}});    //TypeError
     59     Object.defineProperty(o, 'f', { value: 12});     // TypeError
     60 
     61     console.log(o.f); // 10
     62     delete o.f;    // nothing happens     
     63     console.log(o.f); // 10
     64 
     65 
     66     // get, set访问器
     67     // 注意:有get、set的情况下,不能同存在value,writable属性,否则会报错
     68     var variable = 10;
     69     Object.defineProperty(o, 'g', {
     70         get: function() {
     71             return variable;
     72         },
     73         set: function(val) {
     74             variable = val;
     75         }
     76     });
     77 
     78     console.log(o.g);  // 10
     79     o.g = 14;    // set(14)
     80     console.log(o.g);    // 14
     81 
     82 
     83     // 补充:
     84     o.h = 1;
     85     // 相当于:
     86     Object.defineProperty(o, 'h', {
     87         value: 1,
     88         writable: true,
     89         configurable: true,
     90         enumerable: true
     91     }) ;
     92 
     93     Object.defineProperty(o, 'h', { value: 1 });
     94     // 相当于:
     95     Object.defineProperty(o, 'h', {
     96         value: 1,
     97         writable: false,
     98         configurable: false,
     99         enumerable: false
    100     });
  • object.defineProperties(obj, props)

    这个与defineProperty的区别在于一下子可以定义多个属性,就再不展开了。

第一部分暂时到这里结束 : ),感兴趣的同学可以直接去MDN进行了解,请点击这里

posted @ 2014-01-04 10:30  MrPrime  阅读(516)  评论(3编辑  收藏  举报