Javascript循环变量最初是正确的,但随后更改

时间:2016-04-27 20:18:56

标签: javascript loops

==编辑:这是一个最小的例子https://jsfiddle.net/hpb10ao2/2/ ==

当我使用此代码时:

for (var x = 0; x < 50; x+=10) {
    var obj = new window[type]();
    obj.bounds.position = new Vector(x, 0);
    console.log("RECT at " + obj.bounds.position);
    objects.push(obj);
}
for (var i = 0; i < objects.length; i++)
    console.log(objects[i].position());

其中type是&#34; Wall&#34 ;,这是一个带有&#34;边界的对象&#34;具有&#34;位置的房产&#34;属性。 它输出:

output

当我多次使用新的Wall()创建墙壁时,这不会发生,所以它不是该功能的问题,或者我做的任何其他问题(对吗?) 为什么循环后位置变量不同?

矢量函数:http://pastebin.com/4J7S6jbJ

function Vector(x, y) {
    if (x === undefined)
        x = 0;
    if (y === undefined)
        y = 0;
    this.x = x;
    this.y = y;

    this.add = function(x, y) {
        if (y === undefined)
            y = x;
        this.x += x;
        this.y += y;
        return this;
    };
    this.addVector = function(other) {
        this.x += other.x;
        this.y += other.y;
        return this;
    };
    this.subtract = function(x, y) {
        if (y === undefined)
            y = x;
        this.x -= x;
        this.y -= y;
        return this;
    };
    this.subtractVector = function(other) {
        this.x -= other.x;
        this.y -= other.y;
        return this;
    };
    this.multiply = function(x, y) {
        if (y === undefined)
            y = x;
        this.x *= x;
        this.y *= y;
        return this;
    };
    this.multiplyVector = function(other) {
        this.x *= other.x;
        this.y *= other.y;
        return this;
    };
    this.divide = function(x, y) {
        if (y === undefined)
            y = x;
        this.x /= x;
        this.y /= y;
        return this;
    };
    this.divideVector = function(other) {
        this.x /= other.x;
        this.y /= other.y;
        return this;
    };
    this.magnitude = function() {
        return Math.sqrt(this.x * this.x + this.y * this.y);
    };
    this.direction = function() {
        return Math.atan(this.y / this.x);
    };
    this.distance = function(other) {
        return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2));
    };
    this.crossProduct = function(other) {
        return this.x * other.y - this.y * other.x;
    };
    this.normalize = function() {
        if (this.x == 0 && this.y == 0) {}
        else if (this.x == 0)
            this.divide(1, this.magnitude());
        else if (this.y == 0)
            this.divide(this.magnitude(), 1);
        else
            this.divide(this.magnitude());
        return this;
    };

    this.toString = function() {
        return this.x + "," + this.y;
    };
    this.clone = function() {
        return new Vector(this.x, this.y);
    };
    this.equals = function(other) {
        return other.x == this.x && other.y == this.y;
    };
}

1 个答案:

答案 0 :(得分:1)

正如Barmar在评论中指出的,所有Wall实例都使用相同的bounds对象。

你的小提琴

function GameObject() {
    this.bounds = new Rectangle();
}
Test.prototype = new GameObject();
Test.prototype.constructor = Test;
function Test() {
  this.bounds.width = 50;
  this.bounds.height = 50;
}
不会为每个GameObject个实例调用

Test构造函数,您必须在Test构造函数中自行执行此操作

function Test() {
  GameObject.call(this);

查看SLaks' article on js inheritance