在不重復某些值并且只添加所需值initialText的情況下撰寫此函式的最佳方法是什么
createThread = () => {
if (this.props.first) {
publishLow = window.google.createPublish({
readId : this.props.readId
});
} else {
publishLow = window.google.createPublish({
readId : this.props.readId,
// The value needed in this condition
initialText : this.props.initialText
});
}
};
uj5u.com熱心網友回復:
好吧,你可以這樣重構,避免重復
createThread = () => {
const { readId, first, initialText } = this.props;
const payload = { readId };
if (!first) payload.initialText = initialText;
publishLow = window.google.createPublish(payload);
}
還添加了物件解構以使代碼更具可讀性。
uj5u.com熱心網友回復:
我同意 Andy 的觀點,但如果你想讓它變小,我的想法是:
createThread = () => {
const arg = {
readId : this.props.readId
};
if (!this.props.first) {
arg["initialText"] = this.props.initialText;
}
publishLow = window.google.createPublish(arg);
};
uj5u.com熱心網友回復:
createThread = () => {
const { readId, first, initialText } = this.props;
const payload = first ? { readId } : { readId , initialText };
publishLow = window.google.createPublish(payload);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/534240.html
標籤:javascript反应
上一篇:從經過身份驗證的用戶那里檢索角色和權限LaravelBreeze React inertia SpatiePermission
