描述
Object.getOwnPropertyNames() 返回一个数组,该数组对元素是 obj自身拥有的枚举或不可枚举属性名称字符串。 数组中枚举属性的顺序与通过 for…in 循环(或 Object.keys)迭代该对象属性时一致。数组中不可枚举属性的顺序未定义。
示例
使用 Object.getOwnPropertyNames())
var arr = ["a", "b", "c"];console.log(Object.getOwnPropertyNames(arr).sort()); // ["0", "1", "2", "length"]// 类数组对象var obj = { 0: "a", 1: "b", 2: "c"};console.log(Object.getOwnPropertyNames(obj).sort()); // ["0", "1", "2"]// 使用Array.forEach输出属性名和属性值Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) {console.log(val + " -> " + obj[val]);});// 输出// 0 -> a// 1 -> b// 2 -> c//不可枚举属性var my_obj = Object.create({}, {getFoo: {value: function() { return this.foo; },enumerable: false}});my_obj.foo = 1;console.log(Object.getOwnPropertyNames(my_obj).sort()); // ["foo", "getFoo"]
下面的例子演示了该方法不会获取到原型链上的属性:
function ParentClass() {}ParentClass.prototype.inheritedMethod = function() {};function ChildClass() {this.prop = 5;this.method = function() {};}ChildClass.prototype = new ParentClass;ChildClass.prototype.prototypeMethod = function() {};console.log(Object.getOwnPropertyNames(new ChildClass() // ["prop", "method"]));
