0

I've been trying to get multiple functions working in 1 const, but it either does not work (Uncaught SyntaxError: Unexpected identifier) or I'm missing something, and I hope someone can help.

Why does this work:

const dynamicresponse = {
    login(response) {
        alert(response);
    }
}

And why does this not work?

const dynamicresponse = {
    login(response) {
        alert(response);
    }

    adminsearchuser(response) {
        alert(response);
    }
}

And is there a way to get the example above working?

1
  • { login(response) { ... } } is shorthand for { login: function() { ... } } (MDN). As @connexo already mentioned, you're missing commas and therefore get a syntax error. Commented Sep 9, 2018 at 21:36

1 Answer 1

3

You're missing a comma after the body of your login() method.

const dynamicresponse = {
    login(response) {
        alert(response);
    },

    adminsearchuser(response) {
        alert(response);
    }
}

dynamicresponse.login('foo')
dynamicresponse.adminsearchuser('foo')

The notation you tried is used on Javascript class objects.

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

1 Comment

I knew I was probably missing something, but did not know there needs to be a comma at the end. This fixed my issue, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.