简体   繁体   中英

Why am I only getting back the results of my final 'hasOwnProperty' call?

I am just wondering why when I call the 'hasOwnProperty' method multiple times, I am only being returned one boolean value in the console? It is always the final call that returns. The rest of my code is fully functional and if I switch round the order I call to check on where the 3 properties are it returns whichever call came last.

spot.hasOwnProperty("sit");
spot.hasOwnProperty("name");
spot.hasOwnProperty("species"); 

Cheers guys.

They all return but the console just displays output of the latest command; You can put them in an array to see all responses at once

[spot.hasOwnProperty('sit'), spot.hasOwnProperty('name')]

Lacking context, I'm assuming this comes down to just Boolean logic. If you check your actions one at a time you will get the correct value.

 var spot = {}; spot.sit = true; //spot.name = "Spot"; spot.species = "dog"; console.log(spot.hasOwnProperty('sit')); console.log(spot.hasOwnProperty('name')); console.log(spot.hasOwnProperty('species'));

There are 2 options if you are checking all values at once: Boolean AND ( && ) or Boolean OR ( || ).

 var spot = {}; //spot.sit = true; spot.name = "Spot"; spot.species = "dog"; // Boolean OR console.log(spot.hasOwnProperty('sit') || spot.hasOwnProperty('name') || spot.hasOwnProperty('species')); // Boolean AND console.log(spot.hasOwnProperty('sit') && spot.hasOwnProperty('name') && spot.hasOwnProperty('species'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM