0

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
    }
};
3
  • 6
    You can't put arbitrary code in the middle of an object literal. Commented Jan 10, 2014 at 15:54
  • You are randomly throwing in code... Commented Jan 10, 2014 at 15:54
  • You've overlapped two different ways of constructing an object. Commented Jan 10, 2014 at 15:54

3 Answers 3

1

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 () { }
  // ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Glad it helped you. Cheers, from La Paz, Bolivia
0

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
    }
};

Comments

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.