It happens to me lots of times in javascript, php and other languages.
- I need to know if an object exists
- I need to know if that object has a property
- I need to check if the value of that property meets a condition
I use to do like this :
if (typeof myObject != "undefined") {
if (myObject.hasOwnProperty('myProp') {
if (myObject.myProp == "myTestValue") {
// execute code
}
}
}
if I do just like this :
if (myObject.myProp == "myTestValue") {
// execute code
}
it raises an error if object or property doesn't exist.
Is there a way of doing that with one line of code ?
Tks