Hi Gurus
I’m poking around with objects and the differences of creating a variable outside an object and inside an object. Here’s my code.
This is just before the closing body tag:
<script type="text/javascript">
// Creating an object
// Old school way of creating variables - lotsa footprints
var var1 = '';
var var2 = '';
var another = '';
var more = function() {
alert('Alert from the function');
};
// New way of accessing variable - one footprint
var myObj = {
var1 : 'something',
var2 : 'something else',
another : function() {
alert('Hello there');
}
};
alert(myObj.another());
</script>
When i reload the page, i get an alert box stating the 1st message “Hello there”
When i click on OK on the alert prompt. I get another prompt that says Undefined. I tried experimenting and found out that if i call the another() function within myObj without alerting it like so:
myObj.another();
Then i just get an alert prompt with “Hello there” only…the second undefined message doesn’t appear.
Why is this so. I want to really understand this. Thanking you in advance for reading this. ![]()