0

I have an empty object: {}

I'd like to add properties to it. I don't want or need the properties to have keys.

How can I do this?

1
  • you might want to check out scopes and closures. you can in fact create private properties by not exposing their data to a reachable scope. Commented May 26, 2017 at 11:49

1 Answer 1

3

Objects must have property names, otherwise you can't access the properties.

Just use an array instead:

var myObj = [
   'item', 
   'item2'
];

Now just use myObj[0], myObj[1], etc.

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

2 Comments

Noted. However, the challenge still exists: once that object exists, how to I create new anonymous properties on it?
myObj.push('another property').

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.