Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access function/class properties javascript [duplicate]

In the following code, I am having trouble figuring out how to log (in the console) the name and numLegs properties of Penguin (i.e., "emperor" and 2), without changing the inside of the function?

function Penguin(name) {
    this.name = name;
    this.numLegs = 2;
}

var emperor = new Penguin("emperor");

How can I do that?

like image 798
Harman Nieves Avatar asked Oct 27 '25 14:10

Harman Nieves


1 Answers

Simply accessing it's properties. Your emperor is an object, which means that you can access properties with . syntax.

function Penguin(name){

   this.name=name;

   this.numLegs=2;

}

var emperor = new Penguin("emperor");

console.log(emperor.name);
console.log(emperor.numLegs);
like image 97
Suren Srapyan Avatar answered Oct 30 '25 06:10

Suren Srapyan