我想知道是否可以简化以下代码:
this._session = AmberFile.session;
this._sessionPath = AmberFile.sessionPath;
this._sessionDevice = AmberFile.sessionDevice;
this._ssconfig = AmberFile.ssconfig;
我尝试使用TypeScript / ES6,但看起来没有更干净的代码:
const {session, sessionPath, sessionDevice, ssconfig) = AmberFile
this._session = session;
this._sessionPath = sessionPath;
this._sessionDevice = session;
this._ssconfig = sessionPath;
let context: any = {
sessionPath: this._sessionPath,
session: this._session,
sessionDevice: this._sessionDevice
}
答案 0 :(得分:2)
是的。有:
Object.assign(this,{
_session:AmberFile.session,
_sessionPath:AmberFile.sessionPath
})
Object.assign()方法用于将所有可枚举的自身属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。
了解更多:
答案 1 :(得分:2)
const {
session: _session,
sessionPath: _sessionPath,
sessionDevice: _sessionDevice
} = AmberFile;
Object.assign(this, { _session, _sessionPath, _sessionDevice });