What is the most efficient way to declare functions in Javascript?

后端 未结 3 1644
名媛妹妹
名媛妹妹 2021-02-14 15:25

I have always learned that to declare a function in javascript you should do something like:

function myfunction(fruit){
    alert(\'I like \' + fruit + \'!\');
         


        
3条回答
  •  独厮守ぢ
    2021-02-14 16:00

    const

    • provides a read only reference to your function variable, you can see it as a constant function, but don't think of it as an immutable one.

    let

    • allows you to create a local function, meaning that it's scope will be valid only within the enclosing block where it resides.

    MDN references:

    • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
    • https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/const

    Note: if I remember correctly, let and const are not interpreted correctly by all browsers.

提交回复
热议问题