Javascript game development 1: vector

Vectors mathematics belong the basics of every computer game. Transitions, rotations, scalings made with vectors are really simple. Following first class object implements a 2D vector with the most necessary functionality.

(function() {
 
 
    window.objects.vector = (function vector(x = 0.0, y = 0.0) {
        this._x = x;
        this._y = y;
    });
    var vector = window.objects.vector.prototype;
 
    vector._checkParamIsVec = (function(v) {
        return (v.constructor.name === "vector");
    });
 
 
    //get set
    vector.x = (function(x = null) {
        if(x != null)
        {
            this._x = x;
            return true;
        }
        return this._x;
    });
 
    vector.y = (function(y = null) {
        if(y != null)
        {
            this._y = y;
            return true;
        }
        return this._y;
    });
 
    //+
    vector.add = (function(v) {
 
        if(!this._checkParamIsVec(v))
        {
            this._x += v;
            this._y += v;
            return;
        }
 
        this._x += v._x;
        this._y += v._y;
 
    });
 
    //-
    vector.sub = (function(v) {
 
        if(!this._checkParamIsVec(v))
        {
            this._x -= v;
            this._y -= v;
            return;
        }
 
        this._x -= v._x;
        this._y -= v._y;
 
    });
 
    //*
    vector.mul = (function(v) {
 
        if(!this._checkParamIsVec(v))
        {
            this._x *= v;
            this._y *= v;
            return;
        }
 
        this._x *= v._x;
        this._y *= v._y;
 
    });
 
    //:
    vector.div = (function(v) {
 
        if(!this._checkParamIsVec(v))
        {
            this._x /= v;
            this._y /= v;
            return;
        }
 
        this._x /= v._x;
        this._y /= v._y;
 
    });
 
    //length
    vector.length = (function(v) {
 
        if(!this._checkParamIsVec(v)) return;
 
        return Math.sqrt(
            Math.pow(v._x - this._x, 2) + Math.pow(v._y - this._y, 2)
        );
 
    });
 
    //normalize
    vector.normal = (function(v) {
 
        if(!this._checkParamIsVec(v)) return;
        var len = this.length(v);
        return new window.obj.vector(
            (v._x - this._x) / len,
            (v._y - this._y) / len
        );
 
    });
 
 
})();

If you wondering whats meant by “window.objects”, take a look at organize javascript objects. Otherwise window.objects will be undefined. This “class” (Javascript got no classes), implements methods for adding, subtracting, multiplying normalize vectors or for calculating the length of it.
It’s part of the game I’m developing. Later I will explain it in context.

More about vectors can be found here.