user3182431
user3182431

Reputation: 123

Uncaught SyntaxError: Unexpected identifier (Javascript Objects)

I have trouble with my code below.

I have a Object with functions etc and I need a locale storage object too. Is this possible? I always get the "Unexpected identifier" error trying to do this.

var object = {

    var STORAGE = new Object();
    STORAGE.one = null;
    STORAGE.two = null;
    STORAGE.three = null;
    STORAGE.four = null;

    one: function(){
        //function one
    },

    two: function() {
        //function 2
    }
};

Upvotes: 0

Views: 1600

Answers (3)

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18354

What you want is achieved like this:

var object = (function(){
  var STORAGE = new Object();
    STORAGE.one = null;
    STORAGE.two = null;
    STORAGE.three = null;
    STORAGE.four = null;
  return {    
    one: function(){
        //function one
    },

    two: function() {
        //function 2
    }
  };
})();

Cheers

Upvotes: 0

Antoine Reneleau
Antoine Reneleau

Reputation: 38

Is that what you're looking for?

var object = {

    STORAGE: {
        one: null,
        two: null,
        three: null,
        four: null
    },

    one: function(){
        //function one
    },

    two: function() {
        //function 2
    }
};

Upvotes: 0

user229044
user229044

Reputation: 239491

You cannot declare variables or run arbitrary code inside an object literal.

You have to use a property: and a nested object literal:

var object = {

  STORAGE: {
    one: null,
    two: null
    // ...  
  }

  one: function () { }
  // ...
}

Upvotes: 1

Related Questions