我最近开始保持别人的JavaScript代码,我正在修复错误,添加功能,也试图更新代码并使其更加一致。

以前的开发人员使用了两种方式来宣布功能,我无法解决是否有原因。

兩種方式是:

var functionOne = function() {
    // Some code
};
function functionTwo() {
    // Some code
}

使用这两种不同的方法的原因是什么?每个方法的优点和缺点是什么?可以用一种方法做些什么,不能用另一种方法做些什么?


当前回答

function xyz(){
  function abc(){};
  // abc is defined here...
}
// ...but not here

其次,可以结合两种风格:

var xyz = function abc(){};

var xyz = function abc(){
  // xyz is visible here
  // abc is visible here
}
// xyz is visible here
// abc is undefined here

function abc(){};
var xyz = abc;

console.log(xyz === abc); // prints "true"

function abc(){};
console.log(abc.name); // prints "abc"

var abc = function(){};
console.log(abc.name); // prints ""

它的名字是空的 - 我们创建了一个匿名函数,并将其分配给一些变量。

使用合并风格的另一个好理由是使用一个简短的内部名称来引用自己,同时为外部用户提供一个长而不冲突的名称:

// Assume really.long.external.scoped is {}
really.long.external.scoped.name = function shortcut(n){
  // Let it call itself recursively:
  shortcut(n - 1);
  // ...
  // Let it pass itself as a callback:
  someFunction(shortcut);
  // ...
}

在上面的例子中,我们可以用外部名称做同样的事情,但它会太不多(而且更慢)。

function abc(){}

abc在这里在当前范围内在任何地方都被定义:

// We can call it here
abc(); // Works

// Yet, it is defined down there.
function abc(){}

// We can call it again
abc(); // Works

// We can call it here
abc(); // Works
return;
function abc(){}

这是一个功能表达:

var xyz = function(){};

xyz 在这里从分配点定义:

// We can't call it here
xyz(); // UNDEFINED!!!

// Now it is defined
xyz = function(){}

// We can call it here
xyz(); // works

函数声明与函数表达是格雷格所证明的差异的真正原因。

有趣的事实:

var xyz = function abc(){};
console.log(xyz.name); // Prints "abc"

var abc = function(){};

我知道我定义了函数本地,当我定义了函数如

abc = function(){};

function abc(){};

它取决于背景,可能会让你猜测它实际上在哪里定义,特别是在 eval()的情况下 - 答案是: 它取决于浏览器。

其他回答

第一個例子是一個功能宣言:

function abc(){}

第二个例子是函数表达:

var abc = function() {};

在第一個例子中,整個函數宣言都被封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封封。

把它简单地说:

//this will work
abc(param);
function abc(){}

//this would fail
abc(param);
var abc = function() {}

要了解更多关于这个主题,我强烈推荐这个链接。

对格雷格的答案有更好的解释

functionTwo();
function functionTwo() {
}

為什麼沒有錯誤?我們總是被教導,表達從上到底(??)

因为:

函数声明和变量声明总是由JavaScript翻译器无视地移动到其内容范围的顶部。

这就是这样的代码:

functionOne();                  ---------------      var functionOne;
                                | is actually |      functionOne();
var functionOne = function(){   | interpreted |-->
};                              |    like     |      functionOne = function(){
                                ---------------      };

请注意,提交声明的部分未被提交;只有名称被提交。

但是,在函数声明的情况下,整个函数身体也将被包围:

functionTwo();              ---------------      function functionTwo() {
                            | is actually |      };
function functionTwo() {    | interpreted |-->
}                           |    like     |      functionTwo();
                            ---------------

一个函数声明和一个函数表达,在一个变量行为被定义后,相同。

基本上,所有函数声明和变量声明都被插入到声明发生函数的顶部(这就是为什么我们说JavaScript具有函数范围)。

当一个函数声明被拍摄时,函数体“跟随”,因此当函数体被评估时,变量将立即与函数对象联系起来。当一个变量声明被拍摄时,启动不会跟随,而是“左后面”。变量在函数体的开始时被启动到未定义,并将在其原始位置分配一个值。

一些例子......

var foo = 1;
function bar() {
  if (!foo) {
    var foo = 10 }
  return foo; }
bar() // 10

function f() {
  return a; 
  function a() {return 1}; 
  var a = 4;
  function a() {return 2}}
f()() // 2

function f() {
  return a;
  var a = 4;
  function a() {return 1};
  function a() {return 2}}
f()() // 2

函数声明优先于变量声明,最后的函数声明“粘贴”。

function f() {
  var a = 4;
  function a() {return 1}; 
  function a() {return 2}; 
  return a; }
f() // 4

在此例子中, a 与第二个函数声明的评估结果的函数对象开始,然后分配为 4.

var a = 1;
function b() {
  a = 10;
  return;
  function a() {}}
b();
a // 1

我更喜欢将函数定义为变量:

let first = function(x){
   return x[0];
}

而不是:

function first(){
    ....
}

因为我可以在定义函数时使用表达式和装饰器,例如:

let safe = function(f){
  try {f()...}
}
let last = safe(function(x){return x[0]}).

此外,在 ES6 中,它更短:

 let last = x => x[0]
 ...........
 function last(x){
     return x[0];
 }
......

let last = safe(x => x[0]);

您不能在函数声明上使用.bind() 方法,但您可以在函数表达式上使用。

功能宣言:

函数 x() { console.log(this) }.bind('string') x()

功能表达:

var x = 函数() { console.log(this) }.bind('string') x()