I'm having some issues wrapping my head around object instantiation and scope in JavaScript...
Looking at this sample code:
someOtherObj = {
aMethod: function() {
$('body').append('aMethod successfully called!<br>');
return 'd';
}
}
// THIS WORKS!!
$('body').append(someOtherObj.aMethod() + '<br>');
someObj = {
aValue: 'a',
bValue: 'b',
cValue: this.aValue, // THIS DOESN'T WORK!!
dValue: new someOtherObj(), // THIS DOESN'T WORK!!
eValue: {
fValue: aValue, // THIS DOESN'T WORK!!
gValue: this.aValue, // THIS DOESN'T WORK!!
hValue: someObj.aValue, // THIS DOESN'T WORK!!
},
};
// JavaScript crashes out before this, but...
// This should result in: 'a'
$('body').append(someObj.cValue + '<br>');
// This should result in: 'd'
$('body').append(someObj.dValue.aMethod() + '<br>');
// These should result in: 'a'
$('body').append(someObj.eValue.fValue + '<br>');
$('body').append(someObj.eValue.gValue + '<br>');
$('body').append(someObj.eValue.hValue + '<br>');
I think the comments are pretty self explanatory... But that being said:
- How can I make
cValue
reference (===
)aValue
, and/or have the same value (==
) asaValue
? - How can I instantiate a new object as the property of another object?
- How can I access the property of a containing object?