I need to check whether several specific properties exist in an object. And for that I have two questions:
- How am I generally supposed to check for several existing properties in JavaScript?
- Is there a shortcut in CoffeeScript to do this in an elegant way?
My javascript solution would be:
if(obj && obj.pOne && obj.pTwo && obj.pThree) doStuff();
In coffee script that would be no shorter:
if obj and obj.pOne and obj.pTwo and obj.pThree then doStuff()
Don't get mislead here, it's not going to work:
And then I ran into the in operator for if clauses (which I never used for such checks). Is this the right way to do what I want to achieve?
Or is that some sort of no-go, or is there a better/cleaner solution to that?
My code would then look like this:
if(obj && ('pOne' && 'pTwo' && 'pThree' in obj)) doStuff();
and coffeescript like this:
if obj and ('pOne' and 'pTwo' and 'pThree' of obj) then doStuff()
inOPERATOR TOGETHER WITH&&CONCATENATION!!! I just found out the above solution withindoesn't do at all what I want to achieve! It only checks the last property since this is the result of the&&concatenation. I should have known it... seems I have to stick with the first solution, right?