Basic JavaScript - Testing Objects for Properties

Tell us what’s happening:
Describe your issue in detail here.


I am getting frustrated by not getting the logic behind this piece of code.

Besides the fact that i do understand the meaning, can someone have a specific piece of code on this application ?
I am struggling in finding the correct use to it.


  **Your code so far**
function checkObj(obj, checkProp) {
if (obj.hasOwnProperty(checkProp)) {
  return obj[checkProp];
} else {
  return "Not Found";
// Only change code above this line

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

1 Like

The JS engine searches in the prototype of the object if the property isn’t there. So you could mistakenly test for a property.

Take this example

a = { toString: "important property" }
b = { }
b.hasOwnProperty(toString) //false
b.toString ? true : false // true

the last line is true, because b is inheriting a method from “Object” called “toString”. You could just remember the prototype which is inherited from “Object”.

But what when there are several layers of Objects?

For example

let hund = new Dog()

We don’t know which properties hund inherites from Dog’s prototype bc we didn’t set it.

It may not be extremely clear but that is the basic idea, to my understanding.

So the basic wording of the problem is that whenever the prototype and the object have a ‘property-collision’ you may fall into errors.

1 Like

Hi mate,

Thanks for your answer !
I need to dig deeper in this matter.

Have a wonderful day,

You are welcome, take a look at the “prototype” and “inheritance” in JS, it is not the easiest topic but it is worth learning.

1 Like

I will look into it in this instant !

Thanks again, the world would be way better with much people like you :slight_smile:

1 Like

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