JavaScript class - Call method when object initialized

前端 未结 5 1307
时光取名叫无心
时光取名叫无心 2021-02-02 09:56

I have a class similar to the one below. How do I call my init method when the object is created? I don\'t want to have to create an instance of my object then call

5条回答
  •  悲&欢浪女
    2021-02-02 10:33

    There is even more smooth way to do this:

    this.init = function(){
      // method body
    }();
    

    This will both create method and call it.

提交回复
热议问题