Javascript new object (function ) vs inline invocation

后端 未结 3 451
日久生厌
日久生厌 2020-12-30 13:31

Is there any considerations to determine which is better practice for creating an object with private members?

var object = new function () { 
   var private         


        
相关标签:
3条回答
  • 2020-12-30 13:57

    This link provides statistics which also confirms that inline invocation pattern is better.

    Please note that the measurement is in operations per second which the higher the better

    0 讨论(0)
  • 2020-12-30 14:06

    If you're using functions as event handlers you can get memory leaks. Have a look at some of the articles

    0 讨论(0)
  • 2020-12-30 14:08

    The new operator causes the function to be invoked like a Constructor Function.

    I've seen that pattern before, but I don't see any benefits of using it.

    The purpose of the new operator is to create an object (the this value inside the constructor), setting the right [[Prototype]] internal property, to build the prototype chain and implement inheritance (you can see the details in the [[Construct]] operation).

    I would recommend you to stay with the inline invocation pattern.

    0 讨论(0)
提交回复
热议问题