Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass object with changed property

Tags:

javascript

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?

like image 455
Icen Avatar asked Oct 14 '25 15:10

Icen


1 Answers

You 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.

like image 51
Phil Avatar answered Oct 17 '25 06:10

Phil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!