What is the correct way to create a Javascript class?

后端 未结 4 1478
情深已故
情深已故 2021-02-14 20:14

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条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-14 20:37

    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: ... });
    

提交回复
热议问题