你怎么称呼这种JavaScript语法,所以我可以研究它? [英] What do you call this JavaScript syntax, so I can research it?

查看:104
本文介绍了你怎么称呼这种JavaScript语法,所以我可以研究它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)在下面的代码中,使 gameOfLive 变量而不仅仅是函数gameOfLife()

1) In the following code, what is the reasoning behind making gameOfLive a variable and not just function gameOfLife()?

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

2) What is gol? It seems like an array, but I am unfamiliar with the syntax or what its called.

我在学习 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关于函数表达式与函数声明的关系

To quote Juriy ‘kangax’ Zaytsev about 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 是有条件定义的,因此需要使用函数表达式。

In this case, as Joel Coehoorn mentions in a comment, gameOfLife is defined conditionally, so it's needed to use a function expression.

这些有条件定义的函数的一般用例是在没有对较新函数的本机支持的浏览器中增强JavaScript功能(在以前的ECMAScript / JavaScript版本中不可用)。您不希望使用函数声明来执行此操作,因为它们将覆盖本机功能,这很可能不是您想要的(考虑速度等)。一个简短的示例

A general use case for these conditionally defined functions is to enhance JavaScript functionality in browsers that don't have native support for newer functions (not available in previous ECMAScript/JavaScript versions). You don't want to do this using function declarations, as those will overwrite the native functionality anyway, which is most likely not what you want (considering speed, etc.). A short example of this:

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

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

One major drawback of function expressions is that you in fact assign an anonymous function to a variable. This can make debugging harder, as the function name is usually not known when script execution halts (e.g., on a breakpoint you set). Some JavaScript debuggers, like Firebug, try to give the name of the variable the function was assigned to, but as the debugger has to guess this by parsing the script contents on-the-fly, this can be too difficult (which results in a (?)() being shown, instead of a function name) or even be wrong.

(例如,在页面上阅读,虽然它的内容并不完全适合初学者)

(for examples, read on on the page, though its contents are not entirely suitable for beginners)

这篇关于你怎么称呼这种JavaScript语法,所以我可以研究它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆