所有 JavaScript 函数类型? [英] All JavaScript Function Types?

查看:23
本文介绍了所有 JavaScript 函数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为我的小型代码库制作多个测试项目时,我遇到了许多关于以多种不同方式制作函数的教程.

例如:

  1. 函数声明

    • FunctionDeclaration :函数标识符 ( FormalParameterList opt ){ FunctionBody }
  2. 函数表达式

    • FunctionExpression :函数标识符 opt ( FormalParameterList opt ){ FunctionBody }
  3. 匿名自执行函数

    • (函数(味精){警报(味精);})('所以');//提醒SO"
  4. 我在源代码中看到的匿名自执行函数,我怀疑可能不正确,但我想确定

    • (函数(味精){警报(味精);}('所以'));//警告SO",但传递的参数出现在包装函数的括号内,而不是像我认为正确的那样.

说了这么多,我想澄清一下我对每种函数类型的一些问题.

函数声明:

函数显示(){//这个函数在解析时被评估,可以随时调用}

函数声明(在解析时计算并且不需要括号后的括号)是否可以被函数表达式这个问题似乎表明,这个源代码似乎要备份吗?:

var display = function(){//这个函数将被调用而不是//display() 声明(上)任何时候//display() 在这个变量赋值之后使用};

一个函数表达式:

var display = function nameOptional(){};

赋值给变量的函数表达式中括号后的分号是否必要?而且,这是否意味着这个:

函数名(){//...};

这个不同:?

函数名(){//...}//没有分号

第一个 name() 是函数表达式,第二个 name() 是函数声明吗?或者第一个 name() 是一个函数表达式,第二个 name() 是一个带有可选的 left-out 分号的函数表达式.因为我在函数表达式中读到过,所以只推荐使用分号.让我感到困惑的是,在函数声明中,您必须有一个标识符和一个主体.在表达式中,标识符是可选的,但您必须有一个主体.所以如果你有

函数标识符(){//...身体...}

这是声明还是表达式,因为表达式或声明不需要分号.在表达式上,仅建议在变量赋值的情况下获得最佳结果.

匿名自执行函数:

我理解这些是如何工作的,但我见过两种写法,一种我几乎肯定是错误的.我只是想澄清一下:

(函数(味精){警报(味精);})(所以");

(函数(味精){警报(味精);}(所以"));

我已经读到匿名函数执行的原因是因为它被包裹在 () 中,如果你想将任何参数传递给它,它们必须添加在那些 () 并且在一个分号将整个事情联系起来之前,如果这是真的……第二个匿名函数写错了……这是真的吗?

我已经阅读了一些已通过此链接和包含此信息的内容回答的问题

解决方案

只有您在此处确定的前两种类型的函数是真实的".另外两个是函数表达式的应用.

按顺序浏览您的问题:

<块引用>

函数声明(在解析时评估并且不需要括号后的括号)是否可以被函数表达式覆盖,正如这个问题似乎表明的那样,并且这个源代码似乎有备份?

是的.(不幸的是.)第二次声明一个函数会覆盖第一个定义.但是,如果对原始函数的引用已分配给变量,则仍然可以访问原始函数;例如:

function a() { console.log(1);}var a1 = a;函数 a() { console.log(2);}a1();//输出1"一种();//输出2"

<块引用>

赋值给变量的函数表达式中括号后的分号是否必要?

一般来说,是的——它表示语句的结束,就像 var a = 1; 中的分号一样.它有时是可选的,就像 Javascript 中的大多数分号一样,但不建议省略它.

<块引用>

此外,这是否意味着[带有分号的函数声明]与[没有分号的函数声明]不同?

在函数声明的末尾分号不是必需的,也不是有意义的.它实际上被解释为声明之间的独立分号,没有任何作用.

<块引用>

我已经读到匿名函数执行的原因是因为它被包裹在 () 中,如果你想将任何参数传递给它,它们必须在那些 () 之后和分号之前添加如果这是真的……第二个匿名函数写错了……这是真的吗?

他们都很好;这是一种风格选择.该行必须以 function 以外的其他内容开头,这样它就不会被解释为函数定义,因此使用了初始括号,但括号的确切位置并不重要.如果我们用a替换整个函数表达式,那么比较就只是(a)()(a());没有真正的区别.

While making several test projects for my small library of code I have come across many tutorials that go about making functions in many different ways.

For example:

  1. Function Declarations

    • FunctionDeclaration : function Identifier ( FormalParameterList opt ){ FunctionBody }
  2. Function Expressions

    • FunctionExpression : function Identifier opt ( FormalParameterList opt ){ FunctionBody }
  3. Anonymous Self-Executing Functions

    • (function(msg){ alert(msg); })('SO'); // alerts "SO"
  4. Anonymous Self-Executing Functions that I have seen in source code that I suspect may be incorrect but I want to make sure

    • (function(msg){ alert(msg); }('SO')); // alerts "SO", but the passed parameter appears inside the parenthesis that wrap the function, not after like I think is correct.

With all this said, I want to clear the air about a few questions I had regarding each of the types of functions.

A function declaration:

function display(){
    //this function is evaluated at parse time,
      can be called any time
}

Is it true that a function declaration (that is evaluated at parse time and does not require parenthesis after the brackets) can be overwritten by function expressions as this question seems to indicate and this source code seems to back up?:

var display = function(){
                 //this function will be called as opposed to the
                 //display() declaration (above) any time 
                 //display() is used after this variable assignment
              };

A function expression:

var display = function nameOptional(){

              };

Is the semi-colon after the bracket in function expressions that has been assigned to a variable necessary? And also, does that mean that this:

function name(){
    //...
};

is different than this:?

function name(){
    //...
}//no semi-colon

Is the first name() a function expression, and the second name() a function declaration? Or is the first name() a function expression and the second name() a function expression with an optional left-out semi-colon. Because I have read that in function expressions, the semi-colon is only recommended. What confuses me is that in a function declaration you must have an identifier, and a body. In an expression, the identifier is optional, but you must have a body. So if you have

function identifier(){
    //...body...
}

is this a declaration, or an expression since the semi-colon is not required on expressions or declarations. On expressions it is only recommended for optimum results in the case of variable assigning.

An Anonymous Self-Executing Function:

I understand how these work, but I have seen two ways of writing this, one I am almost certain is wrong. I just would like to clear this up:

(function(msg){
    alert(msg);
})("SO");

and

(function(msg){
    alert(msg);
}("SO"));

I have read that the reason an anonymous function executes is because it is wrapped in () and if you want any parameters to be passed to it, they must be added after those () and before a semi-colon tying the whole thing off, if this is true...the second anonymous function is written incorrectly...is this true?

I have read several questions that have been answered with this link and content containing this informatino

解决方案

Only the first two types of functions you've identified here are "real". The other two are applications of function expressions.

Going through your questions in order:

Is it true that a function declaration (that is evaluated at parse time and does not require parenthesis after the brackets) can be overwritten by function expressions as this question seems to indicate and this source code seems to back up?

Yes. (Unfortunately.) Declaring a function a second time overwrites the first definition. The original function may still be accessible if a reference to it has been been assigned to a variable, though; for instance:

function a() { console.log(1); }
var a1 = a;
function a() { console.log(2); }

a1(); // outputs "1"
a();  // outputs "2"

Is the semi-colon after the bracket in function expressions that has been assigned to a variable necessary?

Generally, yes — it denotes the end of the statement, just like the semicolon in var a = 1;. It's sometimes optional, like most semicolons in Javascript, but omitting it is inadvisable.

And also, does that mean that [a function declaration with a semicolon] is different from [a function declaration without one]?

The semicolon is not required, or meaningful, at the end of a function declaration. It's actually interpreted as a standalone semicolon in between the declarations, which has no effect.

I have read that the reason an anonymous function executes is because it is wrapped in () and if you want any parameters to be passed to it, they must be added after those () and before a semi-colon tying the whole thing off, if this is true...the second anonymous function is written incorrectly...is this true?

They're both fine; it's a stylistic choice. The line has to begin with something other than function, so that it's not interpreted as a function definition, so an initial parenthesis is used, but the exact placement of the parentheses isn't significant. If we replace the whole function expression with a, the comparison is simply between (a)() and (a()); there's no real difference.

这篇关于所有 JavaScript 函数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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