1

I know how to access Javascript objects in general, but how to do this if that object is the returned value of a Javascript function that is triggered by default for a certain event?

Simplified example:

$(document).on('change', 'select', function() {

var obj = {
value1: 'val1',
value2: 'val2'
};

return obj;

});

How would I access the returned object?

4
  • 1
    In my opinion, You can't. You need to set it in a variable in the outer scope Commented Oct 2, 2019 at 9:18
  • You cannot - this function will be called as a response to an event but not by you. You can extract the function and call it manually or otherwise tap it, but not with the current implementation Commented Oct 2, 2019 at 9:19
  • Events are not designed to return values it's considered an anti-pattern (in the past people used the return to stop propagation). You cannot in this case and should consider other techniques e.g. triggering your own event, calling a function, writing to variables outside the function (e.g. global store) etc. Better to add what you're trying to achieve. Commented Oct 2, 2019 at 9:22
  • Thanks y'all for your useful comments. Durian. Commented Oct 2, 2019 at 9:41

1 Answer 1

1

You need to create a function

function getValue(value) {
    var obj = {
      value1: value.something,
      value2: value.someting2
    };

    return obj;
}

$(document).on('change', 'select', function(event) {

var result = getValue(event.target.value);

});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.