What is the use of the init() usage in JavaScript?

后端 未结 3 480
星月不相逢
星月不相逢 2021-01-29 20:21

What is the meaning and usage of the init() function in JavaScript?

3条回答
  •  悲哀的现实
    2021-01-29 20:39

    NB. Constructor function names should start with a capital letter to distinguish them from ordinary functions, e.g. MyClass instead of myClass.

    Either you can call init from your constructor function:

    var myObj = new MyClass(2, true); function MyClass(v1, v2) { // ... // pub methods this.init = function() { // do some stuff }; // ... this.init(); // <------------ added this }

    Or more simply you could just copy the body of the init function to the end of the constructor function. No need to actually have an init function at all if it's only called once.

提交回复
热议问题