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

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

兩種方式是:

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

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


当前回答

在JavaScript中,有两种方式可以创建功能:

函数声明:函数 fn(){ console.log(“Hello”); } fn(); 这是非常基本的,自我解释,用在许多语言和标准在C家庭的语言. 我们声明了一个函数定义它,并通过呼叫它执行它. 你应该知道的是,函数实际上是对象在JavaScript; 内部我们创造了一个对象上面的函数,并给它一个名字 cal

参考: JavaScript 函数声明合成: var fn = function() {} vs function fn() {}

其他回答

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()的情况下 - 答案是: 它取决于浏览器。

区别在于,函数One 是一种函数表达式,因此只有在到达该行时才定义,而函数Two 是一种函数声明,并在其周围函数或脚本执行后才定义。

例如,函数表达式:

// TypeError: functionOne 不是函数函数One(); var 函数One = 函数() { console.log(“Hello!”); };

还有一个功能声明:

// 输出: “Hello!”函数Two();函数函数Two() { console.log(“Hello!”); }

“使用严格”; { // 注意此区块! 函数三() { console.log(“Hello!”); } } 函数三(); // 参考错误

这就是所谓的函数表达:

var getRectArea = function(width, height) {
    return width * height;
};

console.log("Area of Rectangle: " + getRectArea(3,4));
// This should return the following result in the console: 
// Area of Rectangle: 12

这就是所谓的功能声明:

var w = 5;
var h = 6;

function RectArea(width, height) {  //declaring the function
  return area = width * height;
}                                   //note you do not need ; after }

RectArea(w,h);                      //calling or executing the function
console.log("Area of Rectangle: " + area);
// This should return the following result in the console: 
// Area of Rectangle: 30

希望这有助于解释函数表达和函数声明之间的区别,以及如何使用它们。

這只是兩種可能的方式來宣告功能,第二種方式,你可以在宣告之前使用功能。

我正在列出下面的差异:

函数声明可以在代码中的任何地方放置。 即使它在定义在代码中出现之前被引用,它也被执行,因为函数声明被承诺记忆或以某种方式被加密,在页面上的任何其他代码开始执行之前。 查看下面的函数: 函数外部函数() {函数 foo() { 返回 1; } 返回 foo(); 函数