在javascript中两个模块声明有什么区别? [英] What is the difference between two declarations of module in javascript?

查看:110
本文介绍了在javascript中两个模块声明有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript中模块的两个声明有什么不同?
一个人在函数周围有括号而另一个没有吗?

What is the different between two declarations of a module in JavaScript? One has parentheses around the function and other one doesn't?

一篇文章说


注意匿名函数周围的()。这是
语言所要求的,因为以令牌函数开头的语句是
,它总是被认为是函数声明。包含()会创建一个
函数表达式。

Notice the () around the anonymous function. This is required by the language, since statements that begin with the token function are always considered to be function declarations. Including () creates a function expression instead.

检查时两者似乎都做同样的事情。

Both seem to do the same thing when checked.

var person = (function () {
    // Private
    var name = "Robert";
    return {
        getName: function() {
            return name;
        },
        setName: function(newName) {
            name = newName;
        }
    };
}());

var person = function () {
    // Private
    var name = "Robert";
    return {
        getName: function() {
            return name;
        },
        setName: function(newName) {
            name = newName;
        }
    };
}();


推荐答案

JavaScript中的函数有两种类型 - 声明和表达式。

Functions are of two types in JavaScript - declarations and expressions.

这是两者之间的差异:


  1. 函数声明已被提升。这意味着您可以在函数出现之前调用该函数,因为JavaScript中的声明悬挂

  2. 可以立即调用函数表达式。函数声明不能​​。这是因为表达式表达(或返回一个值)。函数表达式表示函数。

  1. Function declarations are hoisted. This means you can call the function before it appears in the program as declarations in JavaScript are hoisted.
  2. Function expressions can be invoked immediately. A function declaration cannot. This is because expressions express (or return a value). Function expressions express a function.

函数声明的示例:

foo("bar");

function foo(bar) {
    alert("foo" + bar);
}

上述程序将有效,因为 foo 是一个函数声明。

The above program will work because foo is a function declaration.

foo("bar"); // throws an error, foo is undefined - not a function

var foo = function (bar) {
    alert("foo" + bar);
};

以上程序不能用作 foo 声明为 undefined ,悬挂,然后分配函数表达式的值。因此,当它被调用时,它是 undefined

The above program will not work as foo is declared as undefined, hoisted and then later assigned the value of a function expression. Hence it's undefined when it's called.

函数表达式的一个例子:

(function (bar) {
    alert("foo" + bar);
}("bar"));

上面的函数将立即被调用,因为它是一个函数表达式。

The above function will be immediately invoked as it's a function expression.

function (bar) {
    alert("foo" + bar);
}("bar"); // throws an error, can't call undefined

以上函数不会立即被调用,因为它是功能声明。请记住,声明不表达(或返回值)。所以这就像尝试将 undefined 作为函数调用一样。

The above function will not be immediately invoked as it's a function declaration. Remember, declarations do not express (or return a value). So it's like trying to invoke undefined as a function.

函数如何成为表达式?

如果在预期表达式的上下文中使用函数,则将其视为表达式。否则它被视为声明。

If a function is used in the context where an expression is expected then it's treated as an expression. Otherwise it's treated as a declaration.

表达式预计在以下时间:

Expressions are expected when:


  1. 你'为变量赋值(即 identifier = expression )。

  2. 括号内(即(表达式))。

  3. 作为运算符的操作数(即运算符表达式)。

  1. You're assigning a value to a variable (i.e. identifier = expression).
  2. Inside parentheses (i.e. ( expression )).
  3. As an operand of an operator (i.e. operator expression).

因此以下是所有函数表达式:

Hence the following are all function expressions:

var foo = function () {};
(function () {});
~function () {};

其他一切都是函数声明。简而言之,如果你的函数前面没有任何东西,那就是一个声明。

Everything else is a function declaration. In short if your function is not preceded by anything, it's a declaration.

看到这段代码: https://github.com/aaditmshah/codemirror-repl/blob/master/scripts/index.js#L94

以下函数 isExpression 用于测试某些任意JavaScript代码是否为表达式:

The following function isExpression is used to test whether some arbitrary JavaScript code is an expression or not:

function isExpression(code) {
    if (/^\s*function\s/.test(code)) return false;

    try {
        Function("return " + code);
        return true;
    } catch (error) {
        return false;
    }
}

希望这能解除你的疑虑。

Hope this clears any doubts in your mind.

简而言之:


  1. 函数表达式表示或返回一个值(在本例中为一个函数)。因此可以立即调用它,但在它出现在程序中之前无法调用。

  2. 函数声明是悬挂。因此,它可以在它出现在程序中之前调用。但是,由于它不表示任何值,因此无法立即调用。

  1. A function expression expresses or returns a value (in this case a function). Hence it can be immediately invoked, but it can't be called before it appears in the program.
  2. A function declaration is hoisted. Hence it can be called before it appears in the program. However since it doesn't express any value it can't be immediately invoked.

这篇关于在javascript中两个模块声明有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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