Javascript constructor code is doing weird things, not working properly (Vector)

I have a Vector prototype constructor that looks like this:

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

function CrossProduct(vec1, vec2) {
  return vec1.x * vec2.y - vec1.y * vec2.x;
}

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

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

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

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

  copy: function(other) {
    this.x = other.x;
    this.y = other.y;
    return this;
  },

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

  perp: function() {
    var x = this.x;
    this.x = this.y;
    this.y = -x;
    return this;
  },

  rotate: function(angle) {
    var x = this.x;
    var y = this.y;
    this.x = x * Math.cos(angle) - y * Math.sin(angle);
    this.y = x * Math.sin(angle) + y * Math.cos(angle);
    return this;
  },

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

  normalize: function() {
    var dist = this.len();
    if (dist > 0) {
      this.x = this.x / dist;
      this.y = this.y / dist;
    }
    return this;
  },

  project: function(other) {
    var a = this.dot(other) / other.len2;
    this.x = a * other.x;
    this.y = a * other.y;
    return this;
  },

  projectN: function(other) {
    var a = this.dot(other);
    this.x = a * other.x;
    this.y = a * other.y;
    return this;
  },

  reflect: function(axis) {
    var x = this.x;
    var y = this.y;
    this.project(axis).scale(2);
    this.x -= x;
    this.y -= y;
    return this;
  },

  reflectN: function(axis) {
    var x = this.x;
    var y = this.y;
    this.projectN(axis).scale(2);
    this.x -= x;
    this.y -= y;
    return this;
  },

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

  len2: function() {
    return this.dot(this);
  },

  len: function() {
    return Math.sqrt(this.len2());
  }
};

However, when I invoke it like so:

var vec = new Vector();

It sets the x and y to undefined, regardless of the x || 0 for some reason.

I looked further into it and i went into the console and typed

“Vector”

In order to get the vector function from the window’s console

It returned

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

which is completely different from my code.

Then I investigated further and changed the vector code, by changing the name of the constructor

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

I changed “Vector” to “Vec” and as expected, console brought up a bunch of errors

At this point I am really confused. I tried deleting the file and remaking it, but it still didn’t work.

BTW I am using Carret editor (soz for bad spelling)


Source: stackoverflow-javascript