命名函数声明有什么好处吗? [英] Is there any advantage to naming function declarations?

查看:114
本文介绍了命名函数声明有什么好处吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个。函数foo(){}



B。 var foo = function foo(){}



我以A方式编写所有JavaScript函数,因为我认为B是多余的。我的同事喜欢进去做大规模的寻找/替换来重写我的功能,如B,因为他说这是根据airbnb的最佳做法。


  1. 这两者之间的根本/实际区别是什么?

  2. 有没有危险让他这样做破坏我的代码?


  3. 是一种比另一种更好的做法(你能引用任何来源)吗?

  4. 序言:您称之为函数声明 他被称为命名函数表达式(NFE)。这些在这个问题及其答案中有详细讨论,但是要解决您的问题具体问题:


    ...他说这是根据airbnb的最佳做法...




    如果他在谈论这些Airbnb风格指南,我没有看到任何支持这一说法的东西。我确实以这种方式看到了一些可能 的东西,但这会是误读。它说不要在控制流块中声明函数,例如:

      // bad 
    if(currentUser){
    function test(){
    console.log('Nope。');


    $ / code>

    这是真的,这不仅仅是坏的,它是根据当前的规范无效。您只能在范围顶层声明函数(例如,如果, ,所有控制流结构之外的全局范围内< c $ c>, for 等等;或者在所有控制流结构之外的函数的顶层)。上面的例子是无效的,因为它在连接到如果的块内。 (在ES6中,仅针对Web引擎,它是 tolerated 非常特殊的情况子集 < em> TL; DR:不要这样做 。)



    但是,这不是一个毯子不要使用函数推断,而不是远程。


    两者之间的基本/实际区别是什么?

    您的表单在进入时处理到声明函数的范围,之前执行任何分步代码(有时称为提升)。他的表单在 后面执行,当在代码的分步执行中遇到函数表达式时。



    是的,这个工作代码:

    $ b是否有危险让他破坏我的代码?
    $ b

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



    <如果改写为:

      foo(); //< == Fails 
    var foo = function foo(){
    // ...
    }

    ...因为函数声明提升已经消失( var 提升仍然存在),所以 foo 当您尝试调用它时,变量的值为 undefined

    单独,某些浏览器会收到错误的 ,创建两个函数一个。举例来说,IE8的功能与较早版本的Safari一样。



    最后,他正在重写代码,以依靠自动分号插入的恐怖(要正确地重写它需要在函数体后添加; )。 ASI不完美。依靠它也传统上搞砸了minifiers,虽然现在大多数minifiers正确处理它(从技术上说,不处理它是在缩小器中的错误)。


    是一种比另一种更好的做法(你能引用任何来源)?

    这是一个主观的意见问题,除了上面提到的真实问题(由于拆除吊装和NFEs而中断)之外。

    A. function foo( ){ }

    B. var foo = function foo( ){ }

    I write all my JavaScript functions in the A fashion because I think B is redundant. My coworker likes to go in and do mass find/replace to rewrite my functions like B because he says it's best practice according to airbnb.

    1. What is the fundamental/practical difference between the two?

    2. Is there danger of him breaking my code by doing this?

    3. Is one better practice than the other (can you cite any sources)?


    解决方案

    Preamble: Yours is called a function declaration. His is called a named function expression (NFE). These are discussed in some detail in this question and its answers, but addressing your specific questions:

    ...he says it's best practice according to airbnb...

    If he's talking about these Airbnb style guidelines, I don't see anything there to support that claim. I do see a couple of things you could misread that way, but it would be misreading. It says not to declare functions inside control flow blocks, e.g.:

    // bad
    if (currentUser) {
      function test() {
        console.log('Nope.');
      }
    }
    

    And that's quite true, that's not just bad, it's invalid according to the current specification. You can only declare functions at the top level of a scope (e.g., at global scope outside of all control flow structures like if, while, for, and so on; or at the top level of a function outside of all control flow structures). The example above is invalid because it's within the block connected to the if. (In ES6, for web engines only, it's tolerated in a very specific subset of situations — TL;DR: Don't do it.)

    But that's not a blanket "don't use function declations," not remotely.

    What is the fundamental/practical difference between the two?

    Your form is processed upon entry to the scope in which the function is declared, before any step-by-step code is executed (sometimes this is called "hoisting"). His form is executed later, when the function expression is encountered in the step-by-step execution of the code.

    Is there danger of him breaking my code by doing this?

    Yes, this working code:

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

    ...would break if rewritten as:

    foo(); // <== Fails
    var foo = function foo() {
        // ...
    }
    

    ...because the function declaration hoisting is gone (var hoisting is still there), so the foo variable has the value undefined when you try to call it.

    Separately, some browsers get NFEs wrong, creating two functions instead of one. IE8 does, for instance, as did older versions of Safari. All modern browsers I'm aware of get them right (now).

    Finally, he's rewriting your code to rely on the horror that is Automatic Semicolon Insertion (to properly rewrite it, he'd need to be adding ; after the function body). ASI isn't perfect. Relying on it has also traditionally messed up minifiers, although these days most minifiers handle it properly (since technically, not handling it was a bug in the minifier).

    Is one better practice than the other (can you cite any sources)?

    That's a subjective matter of opinion, other than the real issues raised above (breaking due to removing hoisting, and NFEs).

    这篇关于命名函数声明有什么好处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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