loop throught a function properties, possible?
loop throught a function properties, possible?
I want to loop through a functions properties here is my attempt
var hen = function a() {
this.name = "euler henrique";
this.ID = 55530;
this.REC = 0302;
this.nick = "any will do";
}
for (var p in hen) {
console.log(hen[p]);
}
but that does not work even if hen
was an instance of a. Any suggestion?
hen
hen
var p in new hen
1 Answer
1
You can do it if you create an instance of the object:
var Hen = function (){
this.name ="euler henrique";
this.ID =55530;
this.REC =0302;
this.nick ="any will do";
}
var myHen = new Hen();
for(let prop in myHen){
console.log(prop);
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
hen
is a constructor. use it as such.var p in new hen
– Adelin
5 mins ago