Penguins, Properties, and the Prototype


#1


Why do we not encapsulate console.log(emperor.numLegs); within a pair of { }?


https://www.codecademy.com/en/courses/objects-ii/3/5?curriculum_id=506324b3a7dffd00020bf661#


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

// create your Emperor class here and make it inherit from Penguin
function Emperor(name) {
    this.name = name;
}
Emperor.prototype = new Penguin();
// create an "emperor" object and print the number of legs it has
var emperor = new Emperor("Betty");
console.log(emperor.numLegs);


#2

Why do you think pair of {} would be necessary, what purpose does it serve?


#3

This is why I am asking the question, elv1nas. Would you care to provide an answer?


#4

console.log(emperor.numLegs); is just a simple statement that prints object's property value. I don't understand why you think curly brackets are necessary here. There is always a reason to use them:

To wrap function body, to create object literals and if/else statements, loops and etc.


#5

I thought the curly brackets were necessary because up to this point, the exercises were using them. There was no explanation otherwise in any of the exercises.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.