Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python's hasattr in javascript?

Tags:

javascript

in javascript:

d={one: false, two: true}
d.one
d.two
d.three

I want to be able to differentiate between d.one and d.three. By default they both evaluate to false, but in my case they should not be treated the same.

like image 568
priestc Avatar asked Oct 16 '10 01:10

priestc


People also ask

What is Hasattr () used for?

Python hasattr() function is an inbuilt utility function, which is used to check if an object has the given named attribute and return true if present, else false.

What is Hasattr () used for in Python?

The hasattr() function returns True if the specified object has the specified attribute, otherwise False .

Which method is used to check if an attribute exists or not in Python?

Python hasattr() The hasattr() method returns true if an object has the given named attribute and false if it does not.


1 Answers

You can do

"one" in d // or "two", etc

or

d.hasOwnProperty("one")

You probably want hasOwnProperty as the in operator will also return true if the property is on the an object in the prototype chain. eg.

"toString" in d // -> true

d.hasOwnProperty("toString") // -> false
like image 146
olliej Avatar answered Sep 21 '22 18:09

olliej