Henry wrote:
...
/*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.
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).
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).
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).
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.
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.
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'.
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,
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.
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.
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.
with some hours of javascript learning,
Hours?
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?
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.
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.
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.