【发布时间】:2015-10-24 15:07:01
【问题描述】:
如果我在下面的代码片段中执行测试功能:
function pointInside( r, p ) {
var result =
( p.x >= r.location.x - r.size.width * 0.5 ) &&
( p.x <= r.location.x + r.size.width * 0.5 ) &&
( p.y >= r.location.y - r.size.height * 0.5 ) &&
( p.y <= r.location.y + r.size.height * 0.5 )
;
return result;
}
function test() {
var rect = {};
rect["location"] = { x:6, y:5 };
rect["size"] = { width:10, height:8 };
var p = { x:10, y:8 };
var inside = pointInside( rect, p );
console.log( inside ? "inside" : "outside" );
}
然后文本“inside”被写入控制台。伟大的。现在,如果我将 pointInside 函数更改为:
function pointInside( r, p ) {
return
( p.x >= r.location.x - r.size.width * 0.5 ) &&
( p.x <= r.location.x + r.size.width * 0.5 ) &&
( p.y >= r.location.y - r.size.height * 0.5 ) &&
( p.y <= r.location.y + r.size.height * 0.5 )
;
}
然后当我调用“外部”测试函数时,会被写入控制台。在进一步调查中,我发现 pointInside 函数实际上返回未定义。为什么?我看不出两个版本的 pointInside 之间有任何有意义的区别。谁能给我解释一下?
【问题讨论】:
标签: javascript boolean expression