首页 > Point (1,1) 为什么不能是 Point(0,0) ?

Point (1,1) 为什么不能是 Point(0,0) ?

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
    function Point(x,y) {
        this.x=x;
        this.y=y;
    }
    var p=new Point(1,1);
    Point.prototype.r=function() {
        return Math.sqrt(this.x * this.x + this.y * this.y);
    };
    document.write(p.r());
</script>
</head>
<body>
</body>
</html>

菜鸟一枚, 求大神瞄一眼, 过来调教调教.


你提的问题排版太乱,还有各种不规范,不改一下简直没法看,提问注意排版。


var p=new Point(1,1);

在这个表达式中,Point是构造函数,构造了p这样一个实例对象。p这个实例对象是这样的:

p = {
    x = 1;
    y = 1;
    r = function() {
        return Math.sqrt(p.x * p.x + p.y * p.y);
    };
}
document.write(p.r());    //1.4142135623730951

var p=new Point(0,0);也是可以的,只不过创建的实例p中的属性x = 0; y = 0;最后的结果变成了0.

【热门文章】
【热门文章】