有没有一种方法可以在javascript类中创建仅用于创建变量的方法?

时间:2019-01-12 01:52:00

标签: javascript ecmascript-6

如何避免多种方法使用同一行代码const myObject = this._getMyObject();

我在类中有一个方法_getMyObject(),它返回一个Object[]。我正在其他各种方法中使用它const myObject = this._getMyObject();
有没有办法创建一个方法或返回我可以在其他方法中调用的变量const myObject = this._getMyObject();的东西?

export default class MyClass {
    _getMyObject() {
        //   some code
    }
    _getMySecondObject() {
        //   some code
    }

    _methodOne() {
        const myObject = this._getMyObject();
        const mySecondObject = this._getMySecondObject();
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
    _methodTwo() {
        const myObject = this._getMyObject();
        const mySecondObject = this._getMySecondObject();
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
    _methodThird() {
        const myObject = this._getMyObject();
        const mySecondObject = this._getMySecondObject();
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
}

感谢ritaj解决了我的问题:

    _getLengthOfBoth() {
        const myObject = this._getMyObject();
        const mySecondObject = this._getMySecondObject();
        const lengthOfBoth = myObject.length + mySecondObject.length;
        return lengthOfBoth;
    }

2 个答案:

答案 0 :(得分:0)

我不确定这是否与您的问题有关,但是您可以尝试以下方法:

const obj1 = (src) => src._getMyObject();
const obj2 = (src) => src._getMySecondObject();


export default class MyClass {
    _getMyObject() {
        //   some code
    }
    _getMySecondObject() {
        //   some code
    }

    _methodOne() {
        const myObject = obj1(this);
        const mySecondObject = obj2(this);
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
    _methodTwo() {
        const myObject = obj1(this);
        const mySecondObject = obj2(this);
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
    _methodThird() {
        const myObject = obj1(this);
        const mySecondObject = obj2(this);
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
}

这对于您来说比较少输入字符,obj1obj2可以缩小。

您甚至可以更进一步,为类创建伪私有函数,而不必使用下划线。

const getMyObject = (src) => {
  // The code that used to be in MyClass._getMyObject()
};
const getMySecondObject = (src) => {
  // The code that used to be in MyClass._getMySecondObject()
};


export default class MyClass {
    _methodOne() {
        const myObject = getMyObject(this);
        const mySecondObject = getMySecondObject(this);
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
    _methodTwo() {
        const myObject = getMyObject(this);
        const mySecondObject = getMySecondObject(this);
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
    _methodThird() {
        const myObject = getMyObject(this);
        const mySecondObject = getMySecondObject(this);
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
}

甚至是这样:

const getMyObject = () => {
  // `this` === the object just like when it was a function of the object
  // The code that used to be in MyClass._getMyObject()
};
const getMySecondObject = () => {
  // `this` === the object just like when it was a function of the object
  // The code that used to be in MyClass._getMySecondObject()
};


export default class MyClass {
    _methodOne() {
        const myObject = getMyObject.bind(this)();
        const mySecondObject = getMySecondObject.bind(this)();
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
    _methodTwo() {
        const myObject = getMyObject.bind(this)();
        const mySecondObject = getMySecondObject.bind(this)();
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
    _methodThird() {
        const myObject = getMyObject.bind(this)();
        const mySecondObject = getMySecondObject.bind(this)();
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
}

在此示例中,多亏了bind(this),您才为该类的实例创建了真正的私有函数,并且对getMyObjectgetMySecondObject的每个调用将具有适当的this值。

另一种选择是使用解构:

const getMyObjects = () => {
  // `this` === the object just like when it was a function of the object
  // The code that used to be in
  // MyClass._getMyObject() and  MyClass._getMySecondObject()
  
  return {myObject, mySecondObject};
};


export default class MyClass {
    _methodOne() {
        const {myObject, mySecondObject} = getMyObjects.bind(this)();
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
    _methodTwo() {
        const {myObject, mySecondObject} = getMyObjects.bind(this)();
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
    _methodThird() {
        const {myObject, mySecondObject} = getMyObjects.bind(this)();
        const lengthOfBoth = myObject.length + mySecondObject.length;
        //some code
    }
}

这允许单个函数完成_getMyObject_getMySecondObject中的工作,并在单个对象中返回两个答案。对象的结果将被解构,并放入两个变量myObjectmySecondObject中。

更新

对不起。我写过:

const myObject = getMyObject.bind(this);,当我应该写const myObject = getMyObject.bind(this)();

已解决。

答案 1 :(得分:-1)

使用构造函数

export default class MyClass {
constructor(){
  this.myObject = this._getMyObject();
  this.mySecondObject = this._getMySecondObject();
}

_getMyObject() {
    //   some code
}
_getMySecondObject() {
    //   some code
}

_methodOne() {
    const lengthOfBoth = this.myObject.length + this.mySecondObject.length;
    //some code
}
_methodTwo() {
    const lengthOfBoth = this.myObject.length + this.mySecondObject.length;
    //some code
}
_methodThird() {
    const lengthOfBoth = this.myObject.length + this.mySecondObject.length;
    //some code
}
}