如果我在以下代码片段中执行测试函数:
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 )
;
}
当我调用测试函数时,“outside”被写入控制台。进一步调查后,我发现pointInside函数实际上返回了未定义的值。为什么?我看不出两个版本的pointInside之间有任何有意义的区别。有人能给我解释一下吗?