这行代码超过80个字符-有什么办法可以使它更短?
const playerVars = Object.assign({}, oldSettings.playerVars, newSettings.playerVars);
Here是JSFiddle上的整个项目。
这是包含行的函数:
function combineSettings(oldSettings, newSettings) {
const playerVars = Object.assign({}, oldSettings.playerVars, newSettings.playerVars);
const settings = Object.assign({}, oldSettings, newSettings);
settings.playerVars = playerVars;
return settings;
}
答案 0 :(得分:2)
只需将参数名称更改为较短的名称即可
function combineSettings(oldS, newS) {
const playerVars = Object.assign({}, oldS.playerVars, newS.playerVars);
const settings = Object.assign({}, oldS, newS);
settings.playerVars = playerVars;
return settings;
}
答案 1 :(得分:1)
您还可以为整个"alias"
做一个Object.assign({}, ...args)
函数,并将参数移动到这样的单独行中:
const merge = (...args) => Object.assign({}, ...args)
function combineSettings(oldSettings, newSettings) {
const playerVars = merge(
oldSettings.playerVars,
newSettings.playerVars
);
const settings = merge(oldSettings, newSettings);
settings.playerVars = playerVars;
return settings;
}
这更加简单/简单,可读,并且还添加了merge
作为您以后可以使用的其他功能,等等。
仅供参考,如果您使用的是lodash
merge function already exists以及_.defaults
和_.extend/assignIn
。 注意:显然不建议您为此使用lodash:)
答案 2 :(得分:0)
尽管这不是StackOverflow的目的,但是您可以重构更多:
function combine( ...args ) {
return Object.assign( {}, ...args );
}
function combineSettings( oldSettings, newSettings ) {
return combine( oldSettings, newSettings, {
playerVars : combine( oldSettings.playerVars, newSettings.playerVars )
} );
}
console.log( combineSettings(
{ a : "foo", playerVars : { b : "bar" } },
{ b : "bar", playerVars : { a : "foo" } }
) ); // { a: 'foo', playerVars: { b: 'bar', a: 'foo' }, b: 'bar' }
答案 3 :(得分:0)
为什么不仅仅创建一些变量?像这样
function combineSettings(oldSettings, newSettings) {
const oldSet1 = oldSettings.playerVars;
const newSet1 = newSettings.playerVars;
const playerVars = Object.assign({}, oldSet1, newSet1);
const settings = Object.assign({}, oldSettings, newSettings);
settings.playerVars = playerVars;
return settings;
}
答案 4 :(得分:-1)
我喜欢编码,就像用功能性编程语言编写一样。
所以我对您的问题的解决方案是:
function combineSettings(oldSettings, newSettings) {
return Object.assign(
{},
oldSettings,
newSettings,
{
playerVars:
Object.assign(
{},
oldSettings.playerVars,
newSettings.playerVars
)
}
);
}
const settings1 = {
someSet1: 'a',
playerVars: {
notifications: true,
someVar: 1
}
};
const settings2 = {
someSet1: 'aa',
someSet2: 'b',
playerVars: {
notifications: false,
mail: "somebody@gmail.com"
}
};
console.log(combineSettings(settings1, settings2));
如果要缩小一线,则必须将该变量移动到数组中,然后使用传播运算符调用Object.assign
:
function combineSettings(oldSettings, newSettings) {
var playerVars = [
oldSettings.playerVars,
newSettings.playerVars
];
playerVars = Object.assign({}, ...playerVars); // Your one-liner
// other operations
return {playerVars};
}
const settings1 = {
playerVars: {
notifications: true,
someVar: 1
}
};
const settings2 = {
playerVars: {
notifications: false,
mail: "somebody@gmail.com"
}
};
console.log(combineSettings(settings1, settings2));
OR
将其拆分为行:
function combineSettings(oldSettings, newSettings) {
var playerVars =
Object.assign(
{},
oldSettings.playerVars,
newSettings.playerVars
);
// other operations
return {playerVars};
}
const settings1 = {
playerVars: {
notifications: true,
someVar: 1
}
};
const settings2 = {
playerVars: {
notifications: false,
mail: "somebody@gmail.com"
}
};
console.log(combineSettings(settings1, settings2));
警告::通过将参数名称更改为较短的名称,我们失去了它们的自我说明。 oldSettings
,newSettings
是不言自明的。
因此,通过将长行拆分为多行,可以使代码具有可读性和可扩展性。
我通过查询js style guide
在Google上进行了搜索,并发现了以下可供阅读的好资源:
https://google.github.io/styleguide/jsguide.html
https://github.com/felixge/node-style-guide
额外:大多数IDE都具有美化方法,可以为您完成