Javascript 循环js类的属性
让我们说:Javascript 循环js类的属性,javascript,loops,properties,Javascript,Loops,Properties,让我们说: function person(){ this.name = null; this.lastname = null; this.age = null; this.height = null; } 如何遍历属性 比如: foreach(properties as property){ console.log ( property.nameoftheproperty, property.valueoftheproperty); } 您可以在循
function person(){
this.name = null;
this.lastname = null;
this.age = null;
this.height = null;
}
如何遍历属性
比如:
foreach(properties as property){
console.log ( property.nameoftheproperty, property.valueoftheproperty);
}
您可以在循环中为..使用
,如下所示
function Person(){
this.name = null;
this.lastname = null;
this.age = null;
this.height = null;
}
var person = new Person();
for (var key in person) {
console.log(key, person[key]);
}
输出
name null
lastname null
age null
height null
尝试for(varx in person){x,person[x]}
或Object.keys(person)
for(var prop in properties) {
console.log(properties[prop]);
}