E
emailscotta
After reading Richard's article,
http://www.jibbering.com/faq/faq_notes/closures.html, I would like to
re-visit an earlier post of mine. I think I know understand why the
code doesn't work but would like to get some verification. Below is
some code and below that is an explanation of what I think is
happening.
var SomeObj = {
doSomething : function()
{
return 'Did something';
},
useDoSomething : function()
{
//Doesn't work - doSomething() not defined.
return doSomething();
//Works
//return this.doSomething();
//Works
//return SomeObj.doSomething();
}
}
document.writeln(obj.doSomething());
document.writeln(obj.useDoSomething());
1) Global Variable Instantiation
- A named property of the global object is created, SomeObj.
- But the object is not created until the assignment statement
is executed.
2) Code execution begins
- assignment statement var SomeObj = {....} is executed.
- an execution context is created.
- SomeObj is created with a [[scope]] contain only the
global object.
- Variable instantiation creates 2 named properties
doSomething and useDoSomething.
(Here where I think I'm going wrong)
- doSomething is created with a [[scope]]
containing SomeObj --> global object.
- useDoSomething is created with a [[scope]]
containing SomeObj --> global object.
Since the code does not work SomeObj can not be on the scope chain and
scope chain for doSomething and useDoSomething must only contain the
global object.
I would also like to ask VK to refrain from replying to this message.
It seems that when Richard and VK get together an argument breaks out
and the question get lost. Sorry if this offend either of you I just
want to get my question answer.
Thanks for any help!
http://www.jibbering.com/faq/faq_notes/closures.html, I would like to
re-visit an earlier post of mine. I think I know understand why the
code doesn't work but would like to get some verification. Below is
some code and below that is an explanation of what I think is
happening.
var SomeObj = {
doSomething : function()
{
return 'Did something';
},
useDoSomething : function()
{
//Doesn't work - doSomething() not defined.
return doSomething();
//Works
//return this.doSomething();
//Works
//return SomeObj.doSomething();
}
}
document.writeln(obj.doSomething());
document.writeln(obj.useDoSomething());
1) Global Variable Instantiation
- A named property of the global object is created, SomeObj.
- But the object is not created until the assignment statement
is executed.
2) Code execution begins
- assignment statement var SomeObj = {....} is executed.
- an execution context is created.
- SomeObj is created with a [[scope]] contain only the
global object.
- Variable instantiation creates 2 named properties
doSomething and useDoSomething.
(Here where I think I'm going wrong)
- doSomething is created with a [[scope]]
containing SomeObj --> global object.
- useDoSomething is created with a [[scope]]
containing SomeObj --> global object.
Since the code does not work SomeObj can not be on the scope chain and
scope chain for doSomething and useDoSomething must only contain the
global object.
I would also like to ask VK to refrain from replying to this message.
It seems that when Richard and VK get together an argument breaks out
and the question get lost. Sorry if this offend either of you I just
want to get my question answer.
Thanks for any help!