What is the correct way to create a Javascript class?

后端 未结 4 822
余生分开走
余生分开走 2021-02-14 19:44

I\'m trying to figure out how to construct my Javascript classes (or singleton objects) correctly.

var obj = new Object();
obj.foo = \'bar\';
obj.method = functi         


        
4条回答
  •  旧时难觅i
    2021-02-14 20:42

    If you're targeting a browser or environment that supports ES5, then you can do this:

    var Person = {
      foo: ...,
      bar: ...
    };
    
    var Mike = Object.create(Person, { baz: ... });
    

提交回复
热议问题