|  | simple variables as properties? |  | |
| | | optimistx |  |
| Posted: Tue Sep 02, 2008 1:18 pm Post subject: simple variables as properties? |  |
| |  | |
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? |
| |
| | | Henry |  |
| Posted: Tue Sep 02, 2008 4:38 pm Post subject: Re: simple variables as properties? |  |
| |  | |
On Sep 2, 4:18 pm, optimistx wrote:
| Quote: | 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.
|
It is useful.
| Quote: | 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'] ?
|
You don't.
The "Variable" object belonging to the execution context.
| Quote: | return a1;
}
I am a bit (!) confused: functions are objects, but var a1 inside f is not a property or is it?
|
It is, but it is a property of the execution context's Variable object, which is not directly accessible with javascript.
| Quote: | Variable this.b1 is actually global,
|
Depending on how you call your function (as how a function is called determines the value of - this - ).
| Quote: | 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,
|
You cannot, and don't need to.
| Quote: | if var a1 is inside the function f?
|
There is no sense in which that is true, except lexically.
| Quote: | x['a1'] ? what is x? Or is it impossible?
|
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. |
| |
| | | optimistx |  |
| Posted: Wed Sep 03, 2008 4:50 am Post subject: Re: simple variables as properties? |  |
| |  | |
Henry wrote: ....
| Quote: | 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...
*/ |
| |
| | | Gregor Kofler |  |
| Posted: Wed Sep 03, 2008 6:07 am Post subject: Re: simple variables as properties? |  |
| |  | |
optimistx meinte:
[snip]
| Quote: | 3) 'scope is something obscure, but there is global scope and not-so-global scopes and scope depends on how you come to it'
|
Nope. JS has function scope. That's all.
| Quote: | 1) variable this.b1 inside is different from var b1 outside (* wrong!*)
|
No. Because in your case "this" refers to the global object.
| Quote: | 2) var a1 inside f would be an accessible property of something (*wrong!*)
|
Why? It's only accessible within the matching scope. That's the way to implement private "properties" in JS.
| Quote: | 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?!*)
|
I suppose the answer is "yes".
| Quote: | 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.
|
I doubt it will go without reading. And I'm not sure whether toddlers are interested in client side scripting...
Gregor
-- LINK ::: Landschafts- und Reisefotografie LINK ::: meine JS-Spielwiese LINK ::: Bildagentur für den alpinen Raum |
| |
| | | Jorge |  |
| Posted: Wed Sep 03, 2008 10:20 am Post subject: Re: simple variables as properties? |  |
On Sep 3, 8:50 am, "optimistx" <optimistxPoi...@poistahotmail.com> wrote:
| Quote: | 1) variable this.b1 inside is different from var b1 outside (* wrong!*)
|
this.b1 is the property b1 of the object that 'this' points to. ** in this case **, 'this' points to the global object (this.b1 ==window.b1), and the outside var b1 happens to be === window.b1 (it's a global).
| Quote: | 2) var a1 inside f would be an accessible property of something (*wrong!*)
|
It's an accesible property of an inaccesible object, if you want : you can access it only within f() by its name (a1).
-- Jorge. |
| |
| | | Henry |  |
| Posted: Wed Sep 03, 2008 11:09 am Post subject: Re: simple variables as properties? |  |
| |  | |
On Sep 3, 7:50 am, optimistx wrote:
| Quote: | 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'
|
Broadly irrelevant in this context.
| Quote: | 2) 'this refers to the current object'
|
There is nothing in javascript that provides meaning for the phrase "current object", beyond the trivial and circular; the "current object is the object referred to by the - this - keyword, and the - this - keyword refers to the "current object" (in which there is clearly no point in introducing "current object" as it adds nothing useful).
| Quote: | 3) 'scope is something obscure,
|
No it is not. Scopes consist of chains (or lists) of objects and are 100% predictable/consistent. Each execution context has a scope and each function object has an internal [[Scope]] property that is used to determine the scope for the execution context that come into existence when it is executed. (A function execution context's scope is the scope chain consisting of the chain referred to by the function's [[Scope]] property with a Variable/Activation object added to the end/top of the chain/list).
Note: much uniformed writing on javascript erroneously employs the term 'scope' when talking about the - this - keyword. This is an indication of an author not understanding what they are talking about (with the obvious implications for the veracity of their words).
| Quote: | but there is global scope
|
There is a global execution context, which has a scope, which is a scope chain containing only one object, and that one object is the global object. The global object is also at the end of every other scope chain. The term "global scope" is usually employed as shorthand for the properties of the global object (which, because the global object is at the end of all scope chains, are (unless masked more locally) globally accessible using Identifiers).
| Quote: | and not-so-global scopes and scope depends on how you come to it'
|
No, scopes are lexical; they are determined by the structure of the source code, except where - with - is used to explicitly add arbitrary objects to the scope chain of an execution context.
| Quote: | 4) microsoft, jquery, eval belong to the evil empire and you shoul tell that oftern, amen
|
The last things that will help with programming are mystical beliefs.
| Quote: | I would have guessed that
1) variable this.b1 inside is different from var b1 outside (* wrong!*)
|
Variables declared in the global execution context become properties of the global object because the global execution context uses the global object as its Variable object. In a function called as above the - this - keyword will refer to the global object, and so this.b1 will refer to the 'b1' property of the global object, which is the property created for the variable - b1 - and assigned the value 'outside b1'.
| Quote: | 2) var a1 inside f would be an accessible property of something (*wrong!*)
|
| Quote: | 3) having called f by f() the scope of execution would be the object f,
|
But "object f" is a function object, and there is not really such a thing as a "scope of execution", except possibly the scope belonging to the execution context of a call to f.
| Quote: | all its local variables would belong to the scope, 'local scope' (*wrong?!*)
|
All the local variable do belong to the local scope (in as far as that is meaningful at all); they are named properties of the Variable object, and the Variable object is the object at the top of the execution context's scope chain.
| Quote: | If we pick an average newbie
|
That would not be a useful thing to do because there are two very divergent entry points to learning javascript; the programmer familiar with other languages and designer/HTML author exposed to programming for the first time. the average of those two would be fare from representative of either.
| Quote: | with some hours of javascript learning,
|
Hours?
| Quote: | how would she/he know and learn these effectively and in an easy-to-remember way?
|
Remembering it best encouraged by understanding.
What would be the shape and color of an execution context, a function, a variable?
| Quote: | Lengthy text explanations do not help me.
|
Given sufficient (and assuming authors who know what they are talking about and use correct and consistent terminology) then they probably will help.
| Quote: | 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.
| Quote: | 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...
|
So when you have learnt you can create those, if you still think they would be a good idea. |
| |
| | | optimistx |  |
| Posted: Thu Sep 04, 2008 6:56 am Post subject: Re: simple variables as properties? |  |
Gregor Kofler wrote:
| Quote: | 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
|
That might be a joke, but after checking 'define toddler' with google, I got the impression of toddlers being 1-3 year old children. We probably both agree, that most of them are not interested in learning javascript.
My children were interested in learning pascal in the age of 7-10 years, and wrote small games with that language. |
| |
| | | optimistx |  |
| Posted: Thu Sep 04, 2008 7:41 am Post subject: Re: simple variables as properties? |  |
| |  | |
Henry wrote: ....
| Quote: | 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. |
| |
| | | Henry |  |
| Posted: Thu Sep 04, 2008 10:34 am Post subject: Re: simple variables as properties? |  |
| |  | |
On Sep 4, 10:41 am, optimistx wrote:
| Quote: | 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'
|
Current and context have no special/specific meaning in terms of javascript. There are entities called "execution contexts".
Scope and method are general concepts that are not strictly defined. Scope is effectively defined by the rules relating to the creation and assignment of scope chains and the resolution of identifiers against scope chains. Methods are unhelpfully described as function value properties of objects, but the concept of 'method' only fits when those functions are called in particular ways (as it is how they are called that determines the - this - value).
| Quote: | as they are defined in (list of all the authorized sources of comp.lang.javascript here).
|
ECMA 262 (usually 3rd Ed (for now (3.1 is planed for next year))). No other source is definitive.
| Quote: | 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 :
|
If the word were strictly defined there, but your understanding does appear to differ from the way the defined mechanism works.
| Quote: | '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.
| Quote: | I greatly admire anyone, who in some seconds can reliably see the correct interpretation of those, when looking at any new code sequence.
|
It isn't difficult, the rules are simple. |
| |
| | | Thomas 'PointedEars' Lahn |  |
| Posted: Thu Sep 04, 2008 10:40 am Post subject: Re: simple variables as properties? |  |
| |  | |
Jorge wrote:
| Quote: | On Sep 4, 12:34 pm, Henry <rcornf...@raindrop.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 *** ?
|
No, this would mean that the Global Object was not an object. It could also mean that there were instances where `this' was not preset, which is not the case as well (ES3F, 10.1.7 and 10.2.3).
If we ignored the cases that you mentioned, and also host objects, a simple rule to remember could be that the `this' value refers to the calling object; that is, the object on which the method was called.
PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from <http://www.vortex-webdesign.com/help/hidesource.htm> |
| |
| Page 1 of 3 .:. Goto page 1, 2, 3 Next | |
|
|