Javascript 向函数添加属性并设置其原型

Javascript 向函数添加属性并设置其原型,javascript,prototype,Javascript,Prototype,我必须处理以下代码: function Vector(x, y) { this.x = x || 0; this.y = y || 0; } Vector.add = function(a, b) { return new Vector(a.x + b.x, a.y + b.y); }; Vector.sub = function(a, b) { return new Vector(a.x - b.x, a.y - b.y); }; Vector.scale

我必须处理以下代码:

function Vector(x, y) {
    this.x = x || 0;
    this.y = y || 0;
}

Vector.add = function(a, b) {
    return new Vector(a.x + b.x, a.y + b.y);
};

Vector.sub = function(a, b) {
    return new Vector(a.x - b.x, a.y - b.y);
};

Vector.scale = function(v, s) {
    return v.clone().scale(s);
};

Vector.random = function() {
    return new Vector(
        Math.random() * 2 - 1,
        Math.random() * 2 - 1
    );
};

Vector.prototype = {
    set: function(x, y) {
        if (typeof x === 'object') {
            y = x.y;
            x = x.x;
        }
        this.x = x || 0;
        this.y = y || 0;
        return this;
    },

    add: function(v) {
        this.x += v.x;
        this.y += v.y;
        return this;
    },

    sub: function(v) {
        this.x -= v.x;
        this.y -= v.y;
        return this;
    },

    scale: function(s) {
        this.x *= s;
        this.y *= s;
        return this;
    },

    length: function() {
        return Math.sqrt(this.x * this.x + this.y * this.y);
    },

    lengthSq: function() {
        return this.x * this.x + this.y * this.y;
    },

    normalize: function() {
        var m = Math.sqrt(this.x * this.x + this.y * this.y);
        if (m) {
            this.x /= m;
            this.y /= m;
        }
        return this;
    },

    angle: function() {
        return Math.atan2(this.y, this.x);
    },

    angleTo: function(v) {
        var dx = v.x - this.x,
            dy = v.y - this.y;
        return Math.atan2(dy, dx);
    },

    distanceTo: function(v) {
        var dx = v.x - this.x,
            dy = v.y - this.y;
        return Math.sqrt(dx * dx + dy * dy);
    },

    distanceToSq: function(v) {
        var dx = v.x - this.x,
            dy = v.y - this.y;
        return dx * dx + dy * dy;
    },

    lerp: function(v, t) {
        this.x += (v.x - this.x) * t;
        this.y += (v.y - this.y) * t;
        return this;
    },

    clone: function() {
        return new Vector(this.x, this.y);
    },

    toString: function() {
        return '(x:' + this.x + ', y:' + this.y + ')';
    }
};
这段代码有一个函数
Vector
,该函数附加了一个属性
Vector.add
。然而,下面的几行是
Vector.prototype
,它依次定义
add

现在我们有了
Vector
Vector.add
Vector.prototype.add
,我不太确定调用它们时有什么区别

对于完整参考,我尝试重做的代码来自,因此您可以查看整个代码及其用法


然而,对我来说,作为一个主要使用ECMAScript 6的人,这段代码看起来很奇怪,至少可以这么说。

这是类级函数和实例函数之间的区别,这意味着你应该能够在没有实例的情况下使用Vector.add,而在有实例的情况下使用另一个。

有关静态与原型方法的更多信息,你可以。但就你而言:

静态调用通过将两个向量相加返回一个新向量<代码>v1和
v2
保持不变

let v1 = new Vector(1, 1);
let v2 = new Vector(2, 2);
let v3 = Vector.add(v1, v2);

v1.toString(); // (x:1, y:1)
v2.toString(); // (x:2, y:2)
v3.toString(); // (x:3, y:3)
原型调用将两个向量相加,但不创建新向量;相反,将
v2
x
y
属性添加到
v1

let v1 = new Vector(1, 1);
let v2 = new Vector(2, 2);
v1.add(v2);

v1.toString(); // (x:3, y:3)
v2.toString(); // (x:2, y:2)
注意:prototype方法返回对其被调用的实例的引用,以便可以链接后续调用:

v1.add(v2).sub(v3).add(v4).toString();

若函数附加到Vector.prototype,则它们是实例方法。否则,它们就相当于静态属性请将此更新为答案。如果没有,请等待一些被接受的答案,直到你有了需要发表评论的50个代表我看…然后这就归结为不好的命名…因为它真的很难理解发生了什么…而中文的评论肯定没有多大帮助。谢谢