当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How is this working !function(){console.log("hi")}()

问题描述:

This question already has an answer here:

  • What does the exclamation mark do before the function?

    7 answers

网友答案:

The 'executing' parens at the end can't be done legally after a function expression. A typical (more sensical, IMO) way of writing this is with more parentheses:

(function(){console.log('hi')})()

By prepending the ! before the function expression, the JS interpreter reads the function and then runs it. This is because of the precedence of the ! operator vs. calling a function with the final ()

网友答案:

Look at this answer

tl;dr it defines a function to print out 'hi' and immediately calls it.

分享给朋友:
您可能感兴趣的文章:
随机阅读: