这两种创建对象方法的方法有什么不同吗?

时间:2014-11-04 23:47:17

标签: javascript

这两个对象创建方法的方式有什么不同吗?是否存在性能差异?这有什么区别,如果有的话?

方法1:

var Point = function(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;

    this.add = function(point) {
        this.x += point.x;
        this.y += point.y;
        this.z += point.z;
    };

    this.sub = function(point) {
        this.x -= point.x;
        this.y -= point.y;
        this.z -= point.z;
    };
};

方法2:

var Point = function(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
};

Point.prototype.add = function(point) {
    this.x += point.x;
    this.y += point.y;
    this.z += point.z;
};

Point.prototype.sub = function(point) {
    this.x -= point.x;
    this.y -= point.y;
    this.z -= point.z;
};

0 个答案:

没有答案