Possible Duplicate:
Self-references in object literal declarations
Within a .js file I have an object. I’d like to use some of it’s data within itself. Something like…?
obj = {
thing: 'thing',
things: this.thing + 's'
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can’t create an object that way, however there are a number of alternatives:
-or-
or if you’re using a browser that supports properties:
If you want to know more about them, Jon Resig has a great post about accessors and mutators, AKA getters and setters.
For cross-browser support, stick with a function call for the plural form and only provide an accessor: