I'm trying to pass a variable to a child object of my parent object, but its returning undefined. (clearly I'm not sure how the scoping of a JS object works).
var parent = {
pid: '565',
child: {
cid: this.pid + '_child_789',
sayHello: function(){
alert('My id is ' + this.cid);
}
}
}
Calling parent.child.sayHello()
logs the this.cid
as undefined.
I'm (incorrectly) thinking that the child would inherit the pid property and could be referred to by this.pid
, but it cannot.
Is there a way to do this without hardcoding the pid value?
I know I could do parent.child.cid = parent.pid + '_child_789'
after creating the object but that is not scalable or practical for me.