Objects that does not excist


#1



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


I am on nr 8

I do not understand how the property shorts suddenly aper in the class suitcase. shorts is not defined in class suitcase.......


Replace this line with your code.


#2

HI can you post your code?


#3

Hello @megaplayer16647,

In this exercise, Codecademy introduces you to JavaScript's .hasOwnProperty() method, which checks if the object has such a property.
So in this case, the if statement checks if the suitcase object has the property "shorts", and if it doesn't it sets it to a string of your choice.
Hope this clears things out for you...


#4

var suitcase = {
shirt: "Hawaiian"
};

//console.log(suitcase.shorts);
if (suitcase.hasOwnProperty('shorts')){
console.log(suitcase.shorts);
}
else{
suitcase.shorts = "yellow";
console.log(suitcase.shorts);
}


#5

SO the if statement is false, right?
so it run the else statement

else{
    suitcase.shorts = "yellow"; 
    console.log(suitcase.shorts);
}

this line

suitcase.shorts = "yellow"

this line add propriety short with the value yellow right? So now if you print the suitcase you will have

var suitcase = {
   shirt: "Hawaiian",
   short: "yellow"
}

#6

Thank you everybody. I got the point now.


#7

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