What is return new function(); in JavaScript?

This question already has an answer here:

  • What is the 'new' keyword in JavaScript? 13 answers

  • When we call a function with the new keyword. the following will happen:

  • A new object will be created in the memory
  • The scope of that object will be passed to the function; So the this keyword will refer to that object.
  • The newly created object will be returned.
  • So in essence, that is how you create instances in JavaScript. You need to call a function with the new keyword. When doing so, the function is called constructor.

    In your example, the q function returns an instance of the doStuff method. Bare in mind though that the naming convention is not correct.

    Constructors should be nouns rather that verbs and they should be in Pascal-case, not camel-case

    链接地址: http://www.djcxy.com/p/40852.html

    上一篇: `f()`和`new f()`有什么区别?

    下一篇: 什么是返回新函数(); 在JavaScript中?