icicleking icicleking - 6 months ago 12
Javascript Question

How to pass arguments to setter in javascript object?

I have an object which is a list of filters corresponding to a select list. When the user selects from a drop-down I want update the object using this getter/setter. I'm also using an event listener. I thought the argument would be automatically passed into the handler.

var formFilters = {
audience: 'All',
get audienceFilter() {
return this.audience;
},
set audienceFilter(input) {
$('select').on('change', this.audienceFilter);
}
};


Then to see what's going on:

$('select').on('change', function(e){
console.log(formFilters.audience);
});

Answer

It is pretty straigth forward actually:

formFilters.audienceFilter = 'customValue';

Update: https://jsfiddle.net/7431fz7u/1/

Comments