当我使用“传统”方式时,JavaScript中发生了什么? C风格的函数声明? [英] What's going on in JavaScript when I use the "traditional" C-style function declaration?

查看:105
本文介绍了当我使用“传统”方式时,JavaScript中发生了什么? C风格的函数声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有几种方法可以在JavaScript中定义一个函数。其中两个最常见的是:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ b;


(2)var add = function(a,b){
return a + b;
}

我很喜欢把函数作为可传递对象的想法就像任何其他变量一样。所以我完全理解(2)在做什么。它创建一个函数并赋值给 add (假设这是在全局范围内,所以 add 是一个全局变量)上述功能。但是,如果我使用(1)代替,会发生什么?我已经知道它在执行顺序上有所不同:如果我使用(1),那么我可以参考 add() add()之前的c>,但是如果我使用(2),那么I在我可以开始引用 add()之前,必须将我的函数赋给 add

(1)只是(2)的快捷方式,尽管发生了这种情况像其他C风格的语言一样,让我们​​可以在它的使用点之下定义一个函数?或者它在内部是一种不同类型的功能?哪一个更符合JavaScript的精神(如果这个术语不是太模糊)?你会限制自己的一个或另一个,如果是的话,哪一个?

解决方案

看起来你已经意识到 函数声明的主要特点 1 (1)and 函数表达式 (2)。还要注意,在(1)中仍然有一个局部变量,名为 add ,其中包含一个函数值,就像在(2)中一样:

 函数hello(){
alert('Hello World');
}

console.log(typeof hello); //打印函数

setTimeout(hello,1000); //你仍然可以传递函数作为参数,
//甚至在使用函数声明时也是如此。

另一点值得一提的是函数声明(1)shouldn' t用于有条件地定义函数(如 if 语句),因为正如你所提到的,它们被JavaScript解释器自动移动到包含范围的顶部< SUP> 2 。这通常被称为 提升 em>。



至于哪种方法更符合JavaScript精神,我更喜欢使用函数表达式(2)。对于更权威的观点,道格拉斯克罗克福德列出了坏部分中的函数声明(1)在他热门的好的配件书 2






1 也称为函数语句 =https://stackoverflow.com/users/96100/tim-down> @Tim Down's 的评论如下)。

2 其实一些浏览器可以在中处理函数声明如果语句(请参阅下面的注释)。
3 JavaScript:好的部分 - 附录B:第113页。


I know that there are several ways to define a function in JavaScript. Two of the most common ones are:

(1)  function add (a, b) {
         return a + b;
     }

(2)  var add = function (a, b) {
         return a + b;
     }

I am comfortable with the idea of a function as an object that can be passed around just like any other variable. So I understand perfectly what (2) is doing. It's creating a function and assigning to add (let's say this is in the global scope, so add is a global variable) the said function. But then what is happening if I use (1) instead? I already know that it makes a difference in execution order: if I use (1) then I can refer to add() before the point in the code where add() is defined, but if I use (2) then I have to assign my function to add before I can start referring to add().

Is (1) just a shortcut for (2), albeit one that happens to behave like other C-style languages in allowing us to define a function "below" the point at which it's used? Or is it internally a different type of function? Which is more "in the spirit" of JavaScript (if that isn't too vague a term)? Would you restrict yourself to one or the other, and if so which one?

解决方案

It looks like you are already aware of the main characteristics of function declarations1 (1) and function expressions (2). Also note that in (1) there is still a local variable called add containing a function value, just like in (2):

function hello () {
   alert('Hello World');
}

console.log(typeof hello);  // prints "function"

setTimeout(hello, 1000);    // you can still pass functions around as arguments,
                            // even when using function declarations.

One other point worth mentioning is that function declarations (1) shouldn't be used to define functions conditionally (such as in if statements), because as you have mentioned, they are automatically moved to the top of the containing scope by the JavaScript interpreter2. This is normally referred to as hoisting.

As for which approach is more in the spirit of JavaScript, I prefer using function expressions (2). For a more authoritative opinion, Douglas Crockford lists function declarations (1) in the "Bad Parts" chapter in his popular The Good Parts book2.


1 Also known as function statements (See @Tim Down's comments below).
2 Actually some browsers are able to handle function declarations in if statements (Again refer to comments below).
3 JavaScript: The Good Parts - Appendix B: Page 113.

这篇关于当我使用“传统”方式时,JavaScript中发生了什么? C风格的函数声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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