javascript - 你怎么称呼这个 JavaScript 语法,所以我可以研究它?

标签 javascript conways-game-of-life

1) 在下面的代码中,使 gameOfLive 成为变量而不仅仅是 function gameOfLife() 背后的原因是什么?

2) 什么是gol?它看起来像一个数组,但我不熟悉语法或它的名称。

我正在研究http://sixfoottallrabbit.co.uk/gameoflife/

if (!window.gameOfLife) var gameOfLife = function() {

    var gol = {
        body: null,
        canvas: null,
        context: null,
        grids: [],
        mouseDown: false,
        interval: null,
        control: null,
        moving: -1,
        clickToGive: -1,
        table: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(''),
        tableBack: null,

        init: function(width, height) {
            gol.body = document.getElementsByTagName('body')[0];
            gol.canvas = document.createElement('canvas');
            if (gol.canvas.getContext) {
                gol.context = gol.canvas.getContext('2d');
                document.getElementById('content').appendChild(gol.canvas);
                gol.canvas.width = width;
                gol.canvas.height = height;
                gol.canvas.style.marginLeft = "8px";

                gol.control = document.getElementById('gridcontrol');

                gol.canvas.onmousedown = gol.onMouseDown;
                gol.canvas.onmousemove = gol.onMouseMove;
                gol.canvas.onmouseup = gol.onMouseUp;

                gol.addGrid(48,32,100,44,8);

                gol.refreshAll();
                gol.refreshGridSelect(-1);
                gol.getOptions(-1);

                gol.genTableBack();
            } else {
                alert("Canvas not supported by your browser. Why don't you try Firefox or Chrome? For now, you can have a hug. *hug*");
            }
        },
    }
}

最佳答案

var gameOfLife = function() { }

是一个函数表达式,而

function gameOfLife() { }

是一个函数声明

引用 Juriy ‘kangax’ Zaytsev 关于 Function expressions vs. Function declarations :

There’s a subtle difference in behavior of declarations and expressions.

First of all, function declarations are parsed and evaluated before any other expressions are. Even if declaration is positioned last in a source, it will be evaluated foremost any other expressions contained in a scope. […]

Another important trait of function declarations is that declaring them conditionally is non-standardized and varies across different environments. You should never rely on functions being declared conditionally and use function expressions instead.

在这种情况下,正如 Joel Coehoorn 在评论中提到的那样,gameOfLife 是有条件地定义的,因此需要使用函数表达式。

这些有条件定义的函数的一般用例是增强浏览器中的 JavaScript 功能,这些浏览器不具备对较新函数的 native 支持(在以前的 ECMAScript/JavaScript 版本中不可用)。您不想使用函数声明来执行此操作,因为它们无论如何都会覆盖 native 功能,这很可能不是您想要的(考虑速度等)。短example其中:

if (!Array.prototype.indexOf) { 
    Array.prototype.indexOf = function(item, from) {
        /* implement Array.indexOf functionality,
           but only if there's no native support */
    }
}

函数表达式的一个主要缺点是您实际上将匿名函数分配给变量。这可以使debugging harder ,因为当脚本执行停止时(例如,在您设置的断点上),函数名称通常是未知的。一些 JavaScript 调试器,如 Firebug,尝试给出函数分配给的变量的名称,但由于调试器必须通过即时解析脚本内容来猜测,这可能太困难了(这会导致(?)() 被显示,而不是函数名称)甚至是错误的。

(例如,在页面上继续阅读,但其内容并不完全适合初学者)

关于javascript - 你怎么称呼这个 JavaScript 语法,所以我可以研究它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3208236/

相关文章:

javascript - 为什么我的 minimax 算法不阻止我的 Action ?

javascript - 将 Parse.com 对象转换为字符串

javascript - 重置自定义 jQuery Spinner 的问题

java - 如何用 Java 打印康威生命游戏的棋盘

c - 如何查询邻居的条件?

javascript - jQuery 函数只能在 .ready() 内部工作

javascript - 关闭服务器后 Node-Cron 如何跟踪时间?

c - 使用边界条件导航映射到 1D 的 2D 数组

java - 设计二维世界的最佳解决方案 - 生命游戏

c++ - 生命游戏哈希表 .h 文件模板问题