Basic JavaScript - Testing Objects for Properties

I know. My question was if there was anything in the code I had written that would give a true value from obj.hasOwnProperty(checkProp), due to their being nothing in the code I wrote that assigns checkProp into obj.

Once again not could I provide input that could place checkProp in obj, but if or not it was already somehow placed inside it.

That question doesn’t make sense though. obj.hasOwnProperty(checkProp) only can be evaluated after obj and checkProp have values. Its neither true nor false until then.

So they don’t already contain values, and obj doesn’t automatically contain checkProp?

No. Function parameters don’t have values populated until values are supplied somewhere.

Where would the values come from if the function is never called?

Okay, that’s all I was trying to ask. I just wanted to know if the test was trying to get me to return a true value from obj.hasOwnProperty(checkProp), or write code that could do so with the right argument.

Modify the function checkObj to test if an object passed to the function (obj ) contains a specific property (checkProp ).

I don’t see where this would suggest that you need to force obj to have the checkProp? It involves lots of technical words but seems specific to me.

The last few lines are always the most important for understanding the requirements.

Just to be clear, when you define a function parameter, it is just a placeholder, a label to put on whatever is passed. It doesn’t know what will be passed. For example, this function could have been written:

function checkObj(abrahamLincoln, elephantPajamas) {
  // Only change code below this line
  if (abrahamLincoln.hasOwnProperty(elephantPajamas)) {
    return abrahamLincoln[elephantPajamas];
  } else {
    return "Not Found";
  }
  // Only change code above this line
}

JS can’t read English. It doesn’t know what those mean. And it doesn’t know what it is getting passed. It doesn’t even know what type it is since JS stores types on the value, not the variable. So, it will just get sent a value (for primitives) or a reference (for reference types) and it will store them in those variable names. And then the function can do what it wants with them.

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