admin管理员组文章数量:1394990
There is a function that requires and setting object.
var opt = {
height: 300,
maxLength: 4000,
valueChangeEvent: "change",
placeholder: "",
}
$(selector1).doStuff(opt);
$(selector2).doStuff(opt);
$(selector3).doStuff(opt);
$(selector4).doStuff(opt);
The thing is that the placeholder should be different every time. Currently, I create this opt object with every call but it seems ugly. Is there a way to pass this opt object while just changing one property?
There is a function that requires and setting object.
var opt = {
height: 300,
maxLength: 4000,
valueChangeEvent: "change",
placeholder: "",
}
$(selector1).doStuff(opt);
$(selector2).doStuff(opt);
$(selector3).doStuff(opt);
$(selector4).doStuff(opt);
The thing is that the placeholder should be different every time. Currently, I create this opt object with every call but it seems ugly. Is there a way to pass this opt object while just changing one property?
Share Improve this question edited Oct 4, 2019 at 7:21 SteepLearningCurve 174 bronze badges asked Oct 4, 2019 at 4:35 IcenIcen 4516 silver badges17 bronze badges 5- I am confused. What are you trying to do? "The thing is that placeholder should be different every time" ==> What kind of values should it be? – Spangle Commented Oct 4, 2019 at 4:37
-
What is the actual function you wish to call? (the real value of
doStuff
, that'll indicate what sort of solutions are possible) – CertainPerformance Commented Oct 4, 2019 at 4:37 - @CertainPerformance the function doesn't really matter. It's just about spread operator that I forgot about. – Icen Commented Oct 4, 2019 at 4:51
- Spread works, but I have the strong feeling that it's possible to write even more DRY code, depending on the circumstances. Even with using spread, the code is still pretty repetitive. – CertainPerformance Commented Oct 4, 2019 at 4:53
- @CertainPerformance probably true however doStuff method is just a devExtreeme control called dxTextArea – Icen Commented Oct 4, 2019 at 7:35
4 Answers
Reset to default 6You can use your opt
object as a template like this
$(selector1).doStuff({...opt, placeholder: 'different every time' })
The ...
operator destructures the original opt
object, then any further other properties are added to the result, replacing anything that conflicts.
Use the spread syntax like this:
let opt = {
height: 300,
maxLength: 4000,
valueChangeEvent: "change",
placeholder: "",
}
console.log({...opt, placeholder: "foo"});
console.log({...opt, placeholder: "bar"});
Or Object.assign
like this:
let opt = {
height: 300,
maxLength: 4000,
valueChangeEvent: "change",
placeholder: "",
}
console.log(Object.assign({}, opt, { placeholder: "foo" }));
console.log(Object.assign({}, opt, { placeholder: "bar" }));
Both of these solutions are non-destructive (i.e. they don't change opt
). If you want a destructive solution, leave out the first argument from Object.assign
.
You could write a function which produces opt
for you. In this function, you can pass the placeholder as an argument to be used like so:
const createOpt = (placeholder = '') => ({
height: 300,
maxLength: 4000,
valueChangeEvent: "change",
placeholder
});
console.log(createOpt("x"));
console.log(createOpt("y"));
console.log(createOpt());
you can used spread syntax, allows an iterate are expected in this case:
$(selector1).doStuff({...opt, placeholder: 'Everything' })
read more about it here
本文标签: javascriptPass object with changed propertyStack Overflow
版权声明:本文标题:javascript - Pass object with changed property - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744108820a2591191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论