Timeline for How do I check if an object has a specific property in JavaScript?
Current License: CC BY-SA 3.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
May 23, 2017 at 12:26 | history | edited | URL Rewriter Bot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
Sep 18, 2016 at 8:10 | comment | added | T.J. Crowder |
Object.prototype already has a built-in, correct hasOwnProperty . Overwriting it with an incorrect implementation (1. Properties can have the value undefined , 2. This will give false positives for inherited properties) is just a hideously bad idea. Incorrect answers can and should be deleted. I don't know if you could do that back in Sep '08 when you saw Resig's answer, so commenting to suggest doing it now.
|
|
Sep 30, 2013 at 1:38 | comment | added | Joe Simmons | I think we have a miscommunication. I mean that using your method, it would say that 'toString' is its own property, but it isn't. | |
Sep 29, 2013 at 15:47 | comment | added | enobrev | Not asking to disagree, but to understand. Is there any point where x.hasOwnProperty would return anything besides a boolean true or false? If not, the code as posted should work every time. I suppose maybe if the method were overridden, but then, relying on the result would never be reliable unless you know the overriding method. | |
Sep 28, 2013 at 7:28 | comment | added | Joe Simmons |
I don't think that's good enough. x.hasOwnProperty('toString') === true;
|
|
Aug 23, 2013 at 3:34 | history | edited | Kevin Ji | CC BY-SA 3.0 |
strip the unnecessary parentheses and add a semicolon
|
Sep 25, 2008 at 19:56 | history | edited | enobrev | CC BY-SA 2.5 |
added Konrad's safer solution
|
Sep 25, 2008 at 19:41 | history | edited | enobrev | CC BY-SA 2.5 |
Added hasOwnProperty method and tests
|
Sep 25, 2008 at 19:32 | history | answered | enobrev | CC BY-SA 2.5 |