简体   繁体   English

有人可以向我解释此Javascript Singleton代码吗?

[英]Can someone explain this Javascript Singleton code to me?

this file 这个文件

http://www.iguanademos.com/Jare/docs/html5/Lessons/Lesson2/js/GameLoopManager.js

taken from this site 取自本网站

Here is the code: 这是代码:

            // ----------------------------------------
            // GameLoopManager
            // By Javier Arevalo


            var GameLoopManager = new function() {
                this.lastTime = 0;
                this.gameTick = null;
                this.prevElapsed = 0;
                this.prevElapsed2 = 0;

I understand the declaration of variables, and they are used to record the time between frames. 我了解变量的声明,它们用于记录帧之间的时间。

                this.run = function(gameTick) {
                    var prevTick = this.gameTick;
                    this.gameTick = gameTick;
                    if (this.lastTime == 0)
                    {
                        // Once started, the loop never stops.
                        // But this function is called to change tick functions.
                        // Avoid requesting multiple frames per frame.
                        var bindThis = this;
                        requestAnimationFrame(function() { bindThis.tick(); } );
                        this.lastTime = 0;
                    }
                }

I don't understand why he uses var bindThis = this 我不明白他为什么使用var bindThis = this

                this.stop = function() {
                    this.run(null);
                }

This function set's gameTick to null, breaking the loop in this.tick function. 该函数将gameTick设置为null,从而中断了this.tick函数的循环。

                this.tick = function () {
                    if (this.gameTick != null)
                    {
                        var bindThis = this;
                        requestAnimationFrame(function() { bindThis.tick(); } );
                    }
                    else
                    {
                        this.lastTime = 0;
                        return;
                    }
                    var timeNow = Date.now();
                    var elapsed = timeNow - this.lastTime;
                    if (elapsed > 0)
                    {
                        if (this.lastTime != 0)
                        {
                            if (elapsed > 1000) // Cap max elapsed time to 1 second to avoid death spiral
                            elapsed = 1000;
                            // Hackish fps smoothing
                            var smoothElapsed = (elapsed + this.prevElapsed + this.prevElapsed2)/3;
                            this.gameTick(0.001*smoothElapsed);
                            this.prevElapsed2 = this.prevElapsed;
                            this.prevElapsed = elapsed;
                        }
                        this.lastTime = timeNow;
                    }
                }
            }

Most of this code is what I don't understand, I can see he is recording the time elapsed between frames, but the rest of the code is lost to me. 这些代码大部分是我不了解的,我可以看到他正在记录帧之间经过的时间,但是其余的代码对我来说却是丢失的。

On the website he uses the term singleton, which is used to prevent the program trying to update the same frame twice? 在网站上,他使用术语“单身人士”(singleton)来防止程序尝试两次更新同一帧?

I have a bit of experience with the javascript syntax, but the concepts of singleton, and the general goal/function of this file is lost to me. 我对javascript语法有一些经验,但是单例的概念以及此文件的一般目标/功能对我来说是丢失的。


Why is the above code needed instead of just calling 为什么需要上述代码而不是仅仅调用

requestAnimationFrame(function() {} ); requestAnimationFrame(function(){});

The reason he uses bindThis is that he is passing a method into an anonymous function on the next line. 他使用bindThis的原因是他在下一行将方法传递给匿名函数。 If he merely used this.tick() , this would be defined as the context of requestAnimationFrame . 如果他仅仅用this.tick() this将被定义为的情况下requestAnimationFrame He could achieve the same thing by using call or apply . 他可以通过使用callapply来实现相同的目的。

Singletons are classes that are only instantiated once. 单例是仅实例化一次的类。 This is a matter of practice, and not a matter of syntax - javascript doesn't know what a singleton is. 这是实践问题,而不是语法问题-javascript不知道什么是单例。 By calling it a "Singleton", he is merely communicating that this is a class that is instantiated only once, and everything that needs it will reference the same instance. 通过将其称为“ Singleton”,他只是在传达这是仅实例化一次的类,并且需要它的所有内容都将引用同一实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM