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

查看:20
本文介绍了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*functions/.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天全站免登陆