Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 循环js类的属性_Javascript_Loops_Properties - Fatal编程技术网

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]);
}