Sjak M
Sjak M

Reputation: 63

JavaScript Constants - Defining Multiple Functions

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?

Upvotes: 0

Views: 56

Answers (1)

connexo
connexo

Reputation: 56744

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.

Upvotes: 3

Related Questions