js遍历对象属性,主要有两种方式:1. Object.keys(), 2.for...in
准备一个对象
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
var a = new Foo();
1. Object.keys() // 取出所有OwnProperty(不包含prototype上的key)
Object.keys(a)
> ['a', 'b']
2. for...in // 循环所有Property(包含prototype上的key)
for(var k in a) { console.log(k);}
> a
b
c