What is wrong with this assignment?
base = {};
base.util = {
varA : 3,
varB : this.varA,
show : function(){
console.log(this.varB);
}
};
am getting this.varB is undefined. any help will be appreciated.
Thanks.
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.
When you declare literals, the value of
thiswill only be your object when inside a method. Otherwise, its value will depend on the context where the literal is declared.To accomplish what you want, you have to code it like this: