javascript - I have doubts after reading the authoritative guide to js "as a value function", please clarify.
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-06-30 09:58:52
0
2
590

First of all, I don’t understand function declarations and function expressions very thoroughly. I basically use function declarations, and I know that they will be prefixed. In my mind, I don’t know if the function expression has a function name? Because I haven’t used it much. Today I saw assigning a function to a variable, and found that it seems to be the same as a function expression. I hope someone can answer it, thank you.

var s = function square (x){ return x*x; } console.log(square(4)); console.log(s(4))

The above question 1, the following is question 2, why the above code reports an error "square is not defined", while the following code outputs normally, because the above code belongs to a function expression, so square(4) is not supported Yeah? As I write this, I suddenly want to ask, the function expression is to write on the same line before the function function, and assigning the function to a variable is just based on the function declaration, starting a new line , is that so? Please help, thank you

function square (x){ return x*x; } var s = square; console.log(square(4)); console.log(s(4))
曾经蜡笔没有小新
曾经蜡笔没有小新

reply all (2)
过去多啦不再A梦

Regarding question 2,
expression is to directly create a function as a variable, and the external function does not exist.
When a function is assigned to a variable, the function comes first and then the variable.

So we have the answer to question one. s itself is a function, and this function does not exist externally, so the error does not exist.
In addition, it should be written as:

var s = function() { ....... }

    给我你的怀抱

    Two questions have the same answer. The function name of a function expression is read-only and can only be referenced within this function. It is generally used for recursion.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!