Please help me understand this basic JS concept involving objects and properties

Please help me understand this basic JS concept involving objects and properties
0

#1

Since myObj is not a part of the checkObj function, how can checkProp contain myObj’s properties/values when you call myObj[checkProp]? I’m new to learning JS and this is going a bit over my head.

var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  if(myObj.hasOwnProperty(checkProp)) { 
    return myObj[checkProp]; 
  } else {
    return "Not Found";
  } 
}

#2

MyObj is part of the window scope (in Node its called global scope) in your example, as is the function checkObj. Since they both are a member of the same scope or “closure” the function checkObj can read var myObj.