javascript - Value variable as 0 not as false in shorthand-if expression -


i'm calling function in either 2 ways

foo([x,y]) or foo({x:x,y:y}) x,y ∈ [0,∞)

the foo function looks this

var x = pos.x || pos[0],     y = pos.y || pos[1]; 

if call function second way x=0 pos.x valuated false, leaves x=pos[0] undefined.

i wondered whether there way 0 not valuated false in longhand method if(pos.x===0){/*...*/}

you need check if pos.x exists, rather checking it's value. can hasownproperty function:

var x = pos.hasownproperty('x') ? pos.x : pos[0]; 

Comments

Popular posts from this blog

xml - Extract substrings with XSLT -

How can do a tuple comparison in coffeescript similar to that in python? -

math - "ELO Rating" and how does "TSR Rating" work? -