Assign object to “this”

后端 未结 4 619
余生分开走
余生分开走 2020-12-07 02:30

Say I have a class and some static helper methods like this:

function MyClass (myVar) {
    this.myVar = myVar;

    this.replaceMe = function (value) {
             


        
4条回答
  •  鱼传尺愫
    2020-12-07 03:11

    How about just returning the new instance:

    function MyClass(myVar) {
        // ...
    
        this.replaceMe = function (value) {
            return MyClass.staticHelper(this, value);
        }
    
        // ...
    }
    
    MyClass.staticHelper = function (instance, value) {
        return new MyClass( instance.myVar += value );
    }
    

提交回复
热议问题