Bruno Marques
Bruno Marques

Reputation: 109

Unexpected Token ( in function

I am getting this error:

Error: Line 1: Unexpected token (

in this code:

onInit: function (bus) {
      // Message bus created and ready to be used,
                            window.bus = bus;
                 } 

 };

I cant figure out why I am getting this error? Any help would be appreciated.

Upvotes: 0

Views: 166

Answers (3)

Scott Marcus
Scott Marcus

Reputation: 65873

What you've posted is the syntax for the contents of an Object Literal, but not the outer "shell" of that syntax, so yes, you have an unexpected bit of code as far as the JavaScript runtime is concerned:

onInit: function (bus) {
      // Message bus created and ready to be used,
                            window.bus = bus;
                 } 
};

What you need is to place the "shell" of the object literal around that code:

let someObj = {
  onInit: function (bus) {
  // Message bus created and ready to be used,
    window.bus = bus;
  } 
};

// Now, you can use your object:
someObj.onInit("TEST");
console.log(window.bus);

By the way, creating new properties on the Global window object is rarely a good idea.

Upvotes: 1

ReyHaynes
ReyHaynes

Reputation: 3102

You seem to be missing the opening bracket { if this was suppose to be a function inside an object...

{
  onInit: function (bus) {
    // Message bus created and ready to be used,
    window.bus = bus;
  } 
}

If it's not suppose to be in an object, then one of the following should do:

function onInit (bus) {
  // Message bus created and ready to be used,
  window.bus = bus;
}

or...

var onInit = functtion (bus) {
  // Message bus created and ready to be used,
  window.bus = bus;
}

Upvotes: 0

Magd Kudama
Magd Kudama

Reputation: 3479

Are you trying to do this?

var onInit = function (bus) {
  window.bus = bus;
};

Upvotes: 0

Related Questions