在JavaScript中定义局部函数:use var or not? [英] Define local function in JavaScript: use var or not?

查看:162
本文介绍了在JavaScript中定义局部函数:use var or not?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中声明局部(内部)函数时,有两个选项:

When a local (inner) function is declared in JavaScript, there are two options:

使用 var 关键字,分配给变量:

Declaring with var keyword, assigning to the variable:

(function() {
    var innerFunction1 = function() { ... };
    innerFunction1();
}());

只需使用函数分配给变量:

(function() {
    function innerFunction2() { ... };
    innerFunction2();
}());

我可以看到第二个的优点:该函数可以在调用它的代码下面声明更容易将私有函数与实际执行的代码分离。

I can see one advantage of the second: the function can be declared below the code which calls it so it is easier to separate private functions from the code actually executed.

其中更好为什么

推荐答案

实际上有3种方法来声明函数,

Actually there are 3 ways to declare a function,


  1. 函数声明 :函数声明定义一个命名的函数变量而不需要变量赋值。函数声明作为独立结构发生,不能嵌套在非功能块中。例如: function innerFunction1(){};

  1. Function declaration: A Function Declaration defines a named function variable without requiring variable assignment. Function Declarations occur as standalone constructs and cannot be nested within non-function blocks. ex: function innerFunction1 () { };

Function expression:: A Function Expression defines a function as a part of a larger expression syntax (typically a variable assignment ). Functions defined via Functions Expressions can be named or anonymous.

a。使用匿名函数 - var innerFunction1 = function(){};

a. Using anonymous function - var innerFunction1 = function() { };

使用命名函数 - var innerFunction1 = function myInnerFunction(){};

b. Using named function - var innerFunction1 = function myInnerFunction () { };

功能构造函数:函数构造函数使用Function()构造函数动态定义函数。注意,传递给函数构造函数的函数体是一个字符串。 var innerFunction1 = new Function(arg1,arg2,... argN,functionBody)

Function constructor: A Function Constructor defines a function dynamically using Function( ) constructor. Note that function body passed to the function constructor is a string. var innerFunction1 = new Function (arg1, arg2, ... argN, functionBody)

不推荐使用第三种方法,因为它需要函数体作为字符串,这可能会阻止一些JS引擎优化,并且容易出错。

The 3rd method is not recommended since it needs the function body as a string which may prevent some JS engine optimizations and it is prone to errors.

函数声明和函数表达式的差异是微妙的,你应该选择最适合你的需求的方法。

The differences in function declaration and function expression are subtle and you should choose whichever method suits best for your requirements.

我使用函数表达式,


  1. 我需要单个函数

  2. 确定以编程方式使用的函数(使用命名函数表达式)

函数声明和函数表达式之间的一些区别是

Some differences between function declaration and function expression are,


  1. 函数表达式允许为指定的函数

  2. 可以在函数声明之前使用由函数声明定义的函数,基本上可以在当前范围内的任何地方使用,但是在函数表达式中定义的函数可以在

点击此处阅读函数声明与函数表达式与函数构造函数@MDN 详细比较

注意:函数声明很容易转换为函数表达式。

Note: The function declaration can be easily turned into function expression by assigning to a var.

function foo() {}
alert(foo); // alerted string contains function name "foo"
var bar = foo;
alert(bar); // alerted string still contains function name "foo"

  • http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
  • http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/
  • http://msdn.microsoft.com/en-us/library/ie/x844tc74%28v=vs.94%29.aspx
  • http://es5.github.io/#x15.3
  • JavaScript: var functionName = function() {} vs function functionName() {}

这篇关于在JavaScript中定义局部函数:use var or not?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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