Object.getOwnPropertyNames()

1.Object.getOwnPropertyNames(), traverse instance properties (including non-enumerable), and return an array of property names

var arr = ["a", "b", "c"];
console.log(Object.getOwnPropertyNames(arr).sort()); // ["0", "1", "2", "length"]

// array-like object
var obj = { 0: "a", 1: "b", 2: "c"};
console.log(Object.getOwnPropertyNames(obj).sort()); // ["0", "1", "2"]

// Use Array.forEach to output the property name and property value
Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) {
  console.log(val + " -> " + obj[val]);
});
// output
// 0 -> a
// 1 -> b
// 2 -> c

// non-enumerable properties
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"]

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324548458&siteId=291194637