Basicly, i want “theId” to be equal to what “getId” returns. But how?
var Quiz = {
getId : function() {
var params = 1;
return params;
},
theId : getId(),
};
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.
An object literal can’t refer to properties/methods within itself, because at the time the object literal is evaluated the object doesn’t exist yet. You have to do it as a two-step process:
Note that that sets
theIdto whatevergetId()returned at that time, it doesn’t somehow automatically updatetheIdif your real-world function is more dynamic than your example and potentially returns different values each time it’s called.Alternatively if the function is declared before the object you can create both object properties at once, with one being a reference to the function and the other being the result of calling it.