ECMAScript5之Object学习笔记(三)

第三部分继续...

Object.getOwnPropertyDescriptor(obj, prop)

获取一个对象的属性描述符

根据"Own"这个词我们可以猜到,prop只能是obj的“直接”属性,prototype链上的无效

来几个直观的例子,以作说明:

 1     var person = {},
 2         nameDesc,    // name descriptor        
 3         titleDesc,    // title descriptor
 4         ageDesc;    // age descriptor  
 5 
 6     person.name = 'Andrew';
 7     nameDesc = Object.getOwnPropertyDescriptor(person, 'name');
 8     
 9     console.dir(nameDesc);
10     // { "configurable": true, "enumerable": true, "value": "Andrew", "writable": true }
11 
12 
13     Object.defineProperty(person, 'title', {
14         value: 'sales manager',
15         writable: true
16     });
17     titleDesc = Object.getOwnPropertyDescriptor(person, 'title');
18 
19     console.dir(titleDesc);
20     // { "configurable": false, "enumerable": false, "value": "sales manager", "writable": true }
21 
22 
23     var _age = 25;
24     Object.defineProperty(person, 'age', {
25         get: function() {
26             return _age + ' years old';
27         },
28         set: function(age) {
29             _age = age;
30         }
31     });
32 
33     person.age = 20;
34     console.log(_age);  // 20
35     console.log(person.age)  // 20 years old
36 
37     ageDesc = Object.getOwnPropertyDescriptor(person, 'age');
38     console.dir(ageDesc);
39     // { "configurable": false, "enumerable": false, "get": function () {...}, "set": function (age) {...} }

Object.getOwnPropertyNames(obj)

获取对象的(非原型链上的)“直接”属性名集合(无论该属性是否可列举)

代码示例如下:

 1     var arr = ['1st', '2nd', '3rd'];
 2     console.log( Object.getOwnPropertyNames(arr) );
 3     // ["0", "1", "2", "length"] 
 4 
 5 
 6     var usa = {
 7         president: 'Obama',
 8         states: 52
 9     };
10     console.log( Object.getOwnPropertyNames(usa) );
11     // ["president", "states"] 
12 
13     Object.defineProperty(usa, 'population', {
14         enumerable: false,  
15         value: '300million' 
16     });
17     // 即使属性不可列举,依然能获得
18     console.log( Object.getOwnPropertyNames(usa) );
19     // ["president", "states", "population"] 

Object.getPrototypeOf(obj)

返回对象的prototype

上代码:

 1     var Base = function() {}
 2 
 3     Base.prototype.base_method1 = function() {};
 4     Base.prototype.base_method2 = function() {};
 5 
 6     var base = new Base();
 7 
 8     console.log( Object.getPrototypeOf(base) );
 9     // { base_method1: function, base_method2: function }
10 
11     var Sub = function() {
12         Base.call(this);
13     }
14 
15     Sub.prototype = Object.create(Base.prototype);
16     Sub.prototype.constructor = Sub;
17 
18     Sub.prototype.sub_method1 = function() {};
19 
20     var sub = new Sub();
21     console.log( Object.getPrototypeOf(sub) );
22     // { constructor: function, sub_method1: function, base_method1: function, base_method2: function }

为了方便说明,chrome中输出的内容如下:

从上面的例子,可以发现Object.getPrototypeOf(obj)可以返回对象的prototype,并且通过prototype链(通过__proto__)可以查看到”基类“从”父类“上继承的方法(或属性)。

在chrome中还可以看到返回的prototype对象包括constructor(构造函数)和__proto__(非标准,但现代浏览器都支持,当然IE除外)

Object.keys(obj)

返回对象自身的可列举属性集合

上代码:

 1     var arr = ['Tom', 'Focker', 'linkon'];
 2     console.log( Object.keys(arr) ); 
 3     // ["0", "1", "2"]
 4 
 5 
 6     var person = {
 7         name: 'Andrew',
 8         age: 25,
 9         gender: 'male'
10     };
11     console.log( Object.keys(person) ); 
12     // ["name", "age", "gender"]
13 
14 
15     Object.defineProperty(person, 'address', {
16         value: 'SuZhou,China',
17         enumerable: false  
18     });
19     // address属性不可以列举,所以Object.keys的返回结果不变
20     console.log( Object.keys(person) );
21     // ["name", "age", "gender"]

第三部分就到此为止。

原文地址:https://www.cnblogs.com/AndyWithPassion/p/es5_object_3.html