本文介绍了匿名函数VS const函数 - javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下讨论:


  • ):

      var myFunction =功能(水果){
    alert('我喜欢'+水果);
    }

    和const我的意思是:

      const myfunction =(fruit)=>警报('我喜欢'水果); 

    使用匿名函数或使用const更快吗?我读过使用const允许JavaScript中的编译优化。是否有任何理由为什么我应该使用一个而不是另一个?



    这甚至是相关的吗?


    匿名函数VS常量函数

    假设你正在比较

      var func = function(){}; 
    //或
    let func = function(){};

    with

      const func = function(){}; 

    这样做的主要原因不是优化。它通过代码记录你永远不想改变 func ,并让引擎保护你不会意外地这样做。 (我还应该注意,从ES2015开始,这些函数的 none 都是匿名的,它们都有名称 func ,因为ES2015增加了分配规则名称到通过基于上下文的匿名函数表达式创建的函数,包括像上面这样简单赋值的规则。)



    但是关于优化:



    与大多数JavaScript优化问题一样,答案是: 取决于 。理论上,如果你永远不想改变 func 中的值,那么使用 const 意味着JavaScript引擎可以选择在假设数值永远不会改变的情况下进行优化。请注意, 不会免除引擎处理符号将被嵌套在作用域或类似物中的可能性。



    是否引擎实际上,基于这种知识,通过有意义的方式进行优化,价值不会改变将取决于引擎的实现,并且可能会因引擎而异。



    是否有任何绝对的差异,可以通过常量查找函数来调用它,比通过​​变量查找它更快。

    在程序中转化为实际收益将取决于上述情况,程序结构如何,您使用函数的频率,查找时间与函数实际执行的内容(例如淹没)等的比较等等。 p>

    这取决于。 : - )



    如果您遇到了您认为查找时间导致真实世界问题的情况,请对其进行分析并查看它是否有所帮助。


    重新编辑:

    第一个不是匿名函数。它是一个名为 myFunction 的函数,通过函数声明创建。 (作为声明,最后不需要; )。函数声明不能​​创建匿名函数,名称是声明的必需部分。



    也就是说,它并不重要,因为一旦函数被创建(这发生在与我上面显示的表达式不同的时间),它的行为非常类似于 var func = ... 以 func 的解析方式为例,您是否可以更改 func 等。



    你的第二个例子不同于你的第一个,但是三个重要的方法:


    1. 它将函数引用赋值给一个常量。 >它使用了一个箭头函数,而不是函数函数(这是因为没有更好的术语,我将称之为简单函数)。


    2. 你的箭头函数版本返回调用 alert 的结果(因为你使用的是 简洁的正文 在箭头函数上)。您的声明版本不会。


    我们已经处理了#1的任何性能方面。 #3是,我怀疑,似乎不太可能。



    Re#2(它是一个箭头函数):调用箭头函数需要的工作量少于调用简单函数理论上)。引擎不必设置参数伪对象,不必创建这个绑定。但是如果箭头函数使用这个,它需要更多的工作来查找它(它就像函数关闭的外部范围中的变量) 。但是,这就是理论;只要副作用不明显,引擎就可以优化。例如,如果你的代码中没有使用 arguments ,现代引擎无论如何都避免创建它,即使是简单的函数。我期望这个的箭头函数使用的优化非常好,因为它们看到的 this 不能改变一旦该函数存在。



    所以(等待它):它取决于。


    Following from this discussion:

    I have a doubt about function declaration in JavaScript.

    By anonymous function declaration I mean something like this (https://en.wikibooks.org/wiki/JavaScript/Anonymous_Functions):

    var myFunction = function (fruit){
        alert('I like ' + fruit);
    }
    

    and by const I mean this:

    const myfunction = (fruit) => alert('I like ' fruit);
    

    Is it faster to use an anonymous function, or use a const? I have read that using const allows for compile optimizations in JavaScript. Is there any reason why I should use one instead of the other?

    Is this even relevant?

    解决方案

    Presumably you're comparing

    var func = function() { };
    // or
    let func = function() { };
    

    with

    const func = function() { };
    

    The primary reason for doing that isn't optimization. It's documenting via code that you never intend to change func, and having the engine protect you from accidentally doing so. (I should also note that as of ES2015, none of those functions is anonymous. They all have the name func, because ES2015 adds rules for assigning names to functions created via "anonymous" function expressions based on context, including a rule for simple assignments like the above.)

    But regarding optimization:

    As with most JavaScript optimization questions, the answer is: It depends. In theory, using const if you never mean to change the value in func means that the JavaScript engine has the option of optimizing on the assumption that the value will never change. Note that it doesn't exempt the engine from handling the possibility the symbol will be shadowed in a nested scope or similar.

    Whether the engine actually does optimize in a meaningful way based on that knowledge the value wont' change will depend on the engine's implementation, and likely will vary from engine to engine.

    Whether that makes looking up the function via the constant in order to call it any faster than looking it up via the variable will depend on the engine's implementation, as well.

    Whether any absolute difference translates into actual gains in your program will depend on the above, how your program is structured, how frequently you're using the function, how the lookup time compares with what the function is actually doing (e.g., swamping), etc.

    It depends. :-)

    If you run into a situation where you think the lookup time is causing a real-world problem, profile it and see if it makes a difference.


    Re your edit:

    That first one isn't an anonymous function. It's a function named myFunction, created via a function declaration. (And being a declaration, there's no need for the ; at the end.) Function declarations cannot create anonymous functions, the name is a required part of the declaration.

    That said, it doesn't really matter, as once the function is created (which happens at a different time from the expressions I showed above), it behaves very much like the var func = ... example in terms of how func is resolved, whether you can change func, etc.

    Your second example varies from your first in not one, but three important ways:

    1. It assigns the function reference to a constant.

    2. It uses an arrow function, rather than a function function (which for lack of a better term I'm going to call a "simple" function).

    3. Your arrow function version returns the result of calling alert (because you're using a concise body on the arrow function). Your declaration version doesn't.

    We've already dealt with any performance aspect of #1. #3 is, I suspect, unlikely to matter.

    Re #2 (it being an arrow function): Calling an arrow function requires less work than calling a simple function (in theory). The engine doesn't have to set up the arguments pseudo-object, doesn't have to create a this binding. But if the arrow function uses this, it requires more work to look it up (it's like a variable in the outer scope the function closes over). But again, that's the theory; engines can optimize as long as side-effects are not apparent. For instance, if you don't use arguments in your code, modern engines avoid creating it anyway, even for simple functions. And I'd expect optimization around arrow function usage of this to be pretty good, since the this they see cannot change once the function exists.

    So (wait for it): It depends.

    这篇关于匿名函数VS const函数 - javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:36
查看更多