JavaScript 类 - 对象初始化时调用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3526916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:01:26  来源:igfitidea点击:

JavaScript class - Call method when object initialized

javascriptobject

提问by Ryan Sampson

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

我有一个类似于下面的类。init创建对象时如何调用我的方法?我不想创建我的对象的实例然后像下面那样调用 initialize 。

var myObj = new myClass(2, true);
myObj.init();

function myClass(v1, v2) 
{
    // public vars
    this.var1 = v1;

    // private vars
    var2 = v2;

    // pub methods
    this.init = function() {
        // do some stuff        
    };

    // private methods
    someMethod = function() {
        // do some private stuff
    };
}

回答by Daniel Earwicker

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

注意。构造函数名称应以大写字母开头,以区别于普通函数,例如MyClass代替myClass

Either you can call initfrom your constructor function:

您可以init从构造函数调用:

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 initfunction to the end of the constructor function. No need to actually have an initfunction at all if it's only called once.

或者更简单地说,您可以将init函数体复制到构造函数的末尾。init如果只调用一次,则根本不需要实际拥有一个函数。

回答by Kunok

There is even more smooth way to do this:

有更流畅的方法来做到这一点:

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

This will both create method and call it.

这将创建方法并调用它。

回答by jbeard4

See below for one possible answer, and some corrections to your code.

请参阅下面的一个可能的答案,以及对您的代码的一些更正。

function myClass(v1, v2) 
{
    // public vars
    this.var1 = v1;

    // private vars
    // use var to actually make this private
    var var2 = v2;

    // pub methods
    this.init = function() {
        // do some stuff        
    };

    // private methods
    // this will be private as though it had been declared with var
    function someMethod() {
        // do some private stuff
    };

    //call init
    this.init();
}

回答by harpo

Just add

只需添加

this.init();

to your myClass function.

到您的 myClass 函数。

回答by Ivan

JavaScript classes introduced in ECMAScript 2015 are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance.

- MDN web docs

ECMAScript 2015 中引入的 JavaScript 类主要是对 JavaScript 现有的基于原型的继承的语法糖。类语法并没有向 JavaScript 引入一个新的面向对象的继承模型。JavaScript 类提供了一种更简单、更清晰的语法来创建对象和处理继承。

- MDN 网络文档

When using this syntax, because only the constructor()method is run on instantiation you can't auto-instantiate an object. You always have to add user = new MyUser()

使用此语法时,因为仅constructor()在实例化时运行该方法,所以您无法自动实例化对象。你总是要添加user = new MyUser()

var user;

class MyUser {
  constructor(var1, var2) {

    this.var1 = var1;
    this.var2 = var2;

  }

  static staticMethod() {

    // accessed directly with the class name `MyUser`

  }

  instanceMethod() {

    // accessed with object instance
return true
  }

}

user = new MyUser('hey','there')