如何避免多种方法使用同一行代码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;
}
答案 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
}
}
这对于您来说比较少输入字符,obj1
和obj2
可以缩小。
您甚至可以更进一步,为类创建伪私有函数,而不必使用下划线。
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)
,您才为该类的实例创建了真正的私有函数,并且对getMyObject
和getMySecondObject
的每个调用将具有适当的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
中的工作,并在单个对象中返回两个答案。对象的结果将被解构,并放入两个变量myObject
和mySecondObject
中。
更新:
对不起。我写过:
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
}
}