What is the correct way to create a Javascript class?

后端 未结 4 798
余生分开走
余生分开走 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条回答
  •  长情又很酷
    2021-02-14 20:30

    you can also use something like:

    function O(x,y) {
       this.x=x;
       this.y=y;
       this.method=function() {...}
       return this;
    }
    
    var o=new O(0,0);
    

提交回复
热议问题