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

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -