Hello. How to set something like a constructor inside a function, which will be invariably called when creating an object (there must be a certain sequence of actions and initialization of one variable in the constructor):

var clazz = new myFunction(op); 

It is desirable that it be inside the function:

 function myFunction(op) { constructor(){ ... ... ... ... } } 

Ps. Without the use of libraries.

  • 2
    That is, in this case, myFunction is not a constructor, yes? - user3521
  • updated - knes
  • one
    <small> <i> I recommend reading the book <b> JavaScript DesignPatterns </ b> </ i> </ small> - Zowie

1 answer 1

myFunction and is a constructor.

 function myFunction(op) { this.var1 = ''; //... } 

PS If you want a constructor function EXACTLY that will be called, then ...

  function myFunction(op) { this.__constructor = function(){ this.var1 = ''; } //БИНГО: this.__constructor(); //... }