“new” operator before declaring function

后端 未结 2 1642
面向向阳花
面向向阳花 2021-01-25 09:38

Like explore javascript as my first reference to the whole programming but as i\'m not the professional can\'t comprehend a lot of stuff. So it would be so much appreciated if

2条回答
  •  萌比男神i
    2021-01-25 09:42

    Those are completely different things. The second one is a standard javascript function declaration (and can be called with sayHi();), while the first should be rewritten as

    new (function sayHi() { alert('hi'); });
    

    Now you can see it is a (named) function expression, which is directly passed to the new operator. This should not be used, if you really wanted to call it immediately (there are applications) use (function(){…})());.

提交回复
热议问题