引用'this'比上下文对象更快?

时间:2014-07-29 09:41:06

标签: javascript

我正在为一个画布渲染器重写一个类构造函数,使其只是一个单独的类(我觉得这个项目不需要多个画布)。通过'这个'来引用课程。比在它的功能中引用它的名字更快?我知道引用闭包上下文比全局上下文更快,但好奇的是它作为全局或上下文的一部分而存在。

这是我的课程供参考:

canvasObj = { //canvas initialiser

    init: function(){
        this.el=document.getElement;
        this.c=this.el.getContext('2d');
        this.objs=[];
        this.resetSize();
        this.bAnim=false;
        console.log("canvas initialised");
    },

    clear: function(c){ //fills whole canvas with opaque grey, faster than clearRect()
        if(!c)this.c.fillStyle="#FFFFFF";
        this.c.fillRect(0, 0, this.el.width, this.el.height);
    },

    resetSize: function(){ //resizes  the canvas to window size
        this.el.style.width = window.innerWidth + "px";
        this.el.style.height = window.innerHeight + "px";
        this.el.width = window.innerWidth;
        this.el.height = window.innerHeight;
        this.w=window.innerWidth;
        this.h=window.innerHeight;
        this.o=this.w/this.h;
        console.log("size changed");
    },

    update: function(t){ //runs all stored draw functions
        var i = this.objs.length, selfRef = this, ts = Date.now();
        while(i--){
            this.objs[i](ts-this.ts);
        }
        this.ts=ts;
        if(this.bAnim===true)
            requestAnimationFrame(function(t){selfRef.update(t);});
    },

    addDraw: function(fn){ //adds new draw function to stack
        this.objs.unshift(fn);
    },

    startAnim: function(){ //starts drawing loop
        var selfRef = this;
        this.ts=Date.now();
        this.bAnim=true;
        requestAnimationFrame(function(t){selfRef.update(t);});
    },

    stopAnim: function(){ //stops drawing loop
        this.bAnim=false;
    }
};

0 个答案:

没有答案