![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| ||||||||
| ||||||||
|
|
A variable in global scope var a1 = 'contents of global variable a1'; can be references (with some limitations) as window['a1']; // or window.a1; // or even window['a'+'1']; Thus the variable name a1 can be constructed on the fly, dynamically. Whether that is useful ... at least it is fun. |
|
One could play with selfmodifying code. If the variable is local inside a function var f = function () { var a1 = 'contents of local variable a1 inside f'; this.b1 = 'contents of this.b1 inside f'; // how to refer to variable a1 as a property of something? x['a1'] ? |
|
what is x? |
|
return a1; } I am a bit (!) confused: functions are objects, but var a1 inside f is not a property or is it? |
|
Variable this.b1 is actually global, |
|
at least the variable b1 below was overwritten in a test. var b1 = 'global variable b1 contents'; So, the question is: // how to refer to variable a1 as a property of something, |
|
if var a1 is inside the function f? |
|
x['a1'] ? what is x? Or is it impossible? |
#3
| |||
| |||
|
|
Variable objects are inaccessible. If you need to reference properties of an object that is local to an execution context, create one, assign values to its properties and reference them as properties of that object. /*Thanks for your explanation. To be sure that I understood correctly I try |
#4
| |||
| |||
|
|
1) variable this.b1 inside is different from var b1 outside (* wrong!*) |
|
2) var a1 inside f would be an accessible property of something (*wrong!*) |
#5
| |||||||||||||||||
| |||||||||||||||||
|
|
Henry wrote: ... Variable objects are inaccessible. If you need to reference properties of an object that is local to an execution context, create one, assign values to its properties and reference them as properties of that object. /*Thanks for your explanation. To be sure that I understood correctly I try to repeat with my own words and an example.*/ var a1 ='outside a1'; var b1 ='outside b1'; var f = function () { var a1 ='inside a1'; this.b1 = 'inside b1'; alert('a1 = ' + a1 + '\nb1 = ' + this.b1); } f(); alert(('a1 = ' + a1 + '\nb1 = ' + b1); /* Having learnt that 1) 'functions are objects' |
|
2) 'this refers to the current object' |
|
3) 'scope is something obscure, |
|
but there is global scope |
|
and not-so-global scopes and scope depends on how you come to it' |
|
4) microsoft, jquery, eval belong to the evil empire and you shoul tell that oftern, amen |
|
I would have guessed that 1) variable this.b1 inside is different from var b1 outside (* wrong!*) |
|
2) var a1 inside f would be an accessible property of something (*wrong!*) |
|
3) having called f by f() the scope of execution would be the object f, |
|
all its local variables would belong to the scope, 'local scope' (*wrong?!*) |
|
If we pick an average newbie |
|
with some hours of javascript learning, |
|
how would she/he know and learn these effectively and in an easy-to-remember way? |
|
Pictures? |
|
Lengthy text explanations do not help me. |
|
I understand something, if I can explain it to a 7-10 year old child so that the child explains it to me correctly with his/her own words and pictures. |
|
A collection 'you might think javascript works like this (example with pictures ), but that is wrong!, it works like this (example with pictures) might be useful for learning... |
#6
| |||
| |||
|
|
optimistx meinte: ... I understand something, if I can explain it to a 7-10 year old child so that the child explains it to me correctly with his/her own words and pictures. I doubt it will go without reading. And I'm not sure whether toddlers are interested in client side scripting... Gregor |
#7
| |||
| |||
|
|
On Sep 3, 7:50 am, optimistx wrote: I understand something, if I can explain it to a 7-10 year old child so that the child explains it to me correctly with his/her own words and pictures. If you are only capable of understanding things that can be understood by a 7-10 year old then maybe this subject is beyond you. .... |
#8
| |||||
| |||||
|
|
Henry wrote: On Sep 3, 7:50 am, optimistx wrote: I understand something, if I can explain it to a 7-10 year old child so that the child explains it to me correctly with his/her own words and pictures. If you are only capable of understanding things that can be understood by a 7-10 year old then maybe this subject is beyond you. ... Thanks for your answer. I'll keep that in mind and study carefully. Obviously I have to learn the specific meanings of the words 'scope', 'function', 'object', 'this', 'context', 'current', 'method', 'property', 'activation object', ' 'variable object' |
|
as they are defined in (list of all the authorized sources of comp.lang.javascript here). |
|
I see now that my first understanding of the word 'scope' differs from the strict meaning of the word 'scope' as defined in .../ECMA-262.pdf : |
|
'this' and 'scope' are still quite fuzzy for me. |
|
I greatly admire anyone, who in some seconds can reliably see the correct interpretation of those, when looking at any new code sequence. |
#9
| |||
| |||
|
|
'this' and 'scope' are still quite fuzzy for me. Specific questions might get answered, but if you are looking for someone to write out a detailed explanation for you then you can hope. |
#10
| |||
| |||
|
|
On Sep 4, 12:34 pm, Henry <rcornf... (AT) raindrop (DOT) co.uk> wrote: 'this' and 'scope' are still quite fuzzy for me. Specific questions might get answered, but if you are looking for someone to write out a detailed explanation for you then you can hope. Leaving aside .apply() / .call() and new (Constructors), isn't it that the simple rule to remember is that : The value of 'this' is preset whenever a function is entered, and, 'this' is always preset to the global object *** except when/if the function is called as an object's method *** ? |
![]() |
| Thread Tools | |
| Display Modes | |
| |