2
我有這麼多「這個」。在代碼:「this」的代碼重構。在功能
calculate(){
this.value1 = this.fabrika.calc(this.value2);
this.value4 = this.other.other_calc(this.value10);
// etc
}
我怎麼可以這樣做:
calculate(){
with this{ // error
value1 = fabrika.calc(value2);
value4 = other.other_calc(value10);
}
}
更漂亮的代碼
沒有辦法做到這一點AFAIK,儘管你可以別名'this'爲't'減少長度。 (或者,使用閉包而不是類) – Gerrit0
您可以使用解構語法'let {value2,value10} = this;'將多個值拉入一行中的範圍。儘管如此,任何作業當然不會影響「這個」。 – vu1p3n0x
嗯,有一個真正的['with'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with)關鍵字,但是有一些[強烈的反對使用它的論據](http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/) – Saravana