473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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?

Sep 2 '08 #1
26 1732
On Sep 2, 4:18 pm, optimistx wrote:
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.
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.
what is x?
The "Variable" object belonging to the execution context.
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.
Variable this.b1 is actually global,
Depending on how you call your function (as how a function is called
determines the value of - this - ).
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.
if var a1 is inside the function f?
There is no sense in which that is true, except lexically.
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.
Sep 2 '08 #2
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...

*/
Sep 3 '08 #3
optimistx meinte:

[snip]
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.
1) variable this.b1 inside is different from var b1 outside (* wrong!*)
No. Because in your case "this" refers to the global object.
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.
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".
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
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Sep 3 '08 #4
On Sep 3, 8:50*am, "optimistx" <optimistxPoi...@poistahotmail.com>
wrote:
>
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).
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.
Sep 3 '08 #5
On Sep 3, 7:50 am, optimistx wrote:
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.
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.
Pictures?
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.
Sep 3 '08 #6
Gregor Kofler wrote:
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.
Sep 4 '08 #7
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.

Sep 4 '08 #8
On Sep 4, 10:41 am, optimistx wrote:
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).
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.
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.
'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.
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.
Sep 4 '08 #9
On Sep 4, 12:34*pm, Henry <rcornf...@raindrop.co.ukwrote:
>
'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 *** ?

--
Jorge.
Sep 4 '08 #10
Jorge wrote:
On Sep 4, 12:34 pm, Henry <rcornf...@raindrop.co.ukwrote:
>>'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>
Sep 4 '08 #11
On Sep 4, 2:40*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Jorge wrote:
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
Hmm, why ?

--
Jorge.
Sep 4 '08 #12
On Sep 4, 1:17 pm, Jorge wrote:
On Sep 4, 12:34 pm, Henry 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 *** ?
The rule is simple, but not quite as simple as that suggests. The
problem being that "called as an object's method" has little technical
meaning in terms of the specification.

You mean that when the item to the left of the parenthesise of what
may be called the 'call operator' is a property accessor then the
object that is the evaluated result of the part of the property
accessor prior to the final dot, or final set of brackets, becomes the
- this - value in the resulting function call. This would be true, but
is a little short of the whole picture.

Technically, when the right hand side of a CallExpression evaluates as
a Reference type and the - base - of that Reference type is non-null
and is not an 'Activation object' then that object becomes the value
used for - this - (else the global object is used instead). Property
accessors always evaluate to Reference types with non-null - base -
properties (or error, in which case you never get to the function call
question), but there are other possibilities.

Unless the process errors, Identifier resolution also always evaluates
to a Reference type. In those cases function local variables
(including a function's parameters and inner function declarations)
will result in a reference type with a Variable object as its - base
-, and as function execution contexts use their Activation object as
their Variable object those Reference types will not result in the -
base - being used for - this -. However, Identifier resolution can
happen within a - with - statement, and if the Identifier corresponds
with a named property of the object added to the scope chain with the
- with - statement then the Reference type will have the object added
to the scope chain as its - base -, and as that object is not an
Activation object it will become the - this - value for the function
call. To illustrate:-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
(function(){
function test1(n){
alert(n+' test ='+this.exName);
}
with(
{
test2:test1,
exName:'Object added to scope chain.'
}
){
test1('1'); //Expecting: '1 test =undefined'
test2('2'); //Expecting: '2 test =Object added to scope chain.'
}
})();
</script>
</body>
</html>

- where - test1 - and the - test2 - property of the object added to
the scope chain are the same function, and the code used in each call
has the same form/structure. Neither call looks like what could be
named a "method call", yet the - test2('2'); - call is setting the -
this - value to the object added to the scope chain, so it must be a
"method call" in some sense. And if - test('2'); - is a method call
then how can - test1('1'); - not be considered to be one? The
distinction certainly cannot be observed from the CallExpressions
themselves. The distinction, and so what must be the definition of a
"method call", is the value of a possible left hand side Reference
type and its - base -.
Sep 4 '08 #13
Jorge wrote:
Thomas 'PointedEars' Lahn wrote:
>Jorge wrote:
>>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

Hmm, why ?
If you are referring to my first sentence: See Richard's followup for
details. In short, it would mean that because globally declared functions
are methods of the Global Object (they become it per variable instantiation
of the global execution context where the Global Object is the Variable
Object). And they are called as such through the algorithm of "Identifier
and Scope Chain Resolution" (see ES3F) even when the Global Object is not
being referred to explicitly (per `this', or an expression that resolves to
a property to hold the reference, through that algorithm).

If you are referring to my second one: Because one could read your
statement-question as "The value of 'this' is preset whenever [something
applies] *** except when/if the function is called as an object's method ***".
HTH

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>
Sep 4 '08 #14
In comp.lang.javascript message <c96a80e0-10d0-4d4f-9e6c-60139b328829@n3
8g2000prl.googlegroups.com>, Thu, 4 Sep 2008 03:34:08, Henry
<rc*******@raindrop.co.ukposted:
>ECMA 262 (usually 3rd Ed (for now (3.1 is planed for next year))). No
other source is definitive.
Not so. ISO/IEC 16262 is superior.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Sep 4 '08 #15
On Sep 4, 4:12*pm, Henry <rcornf...@raindrop.co.ukwrote:
(...) Neither call looks like what could be
named a "method call", yet the - test2('2'); - call is setting the -
this - value to the object added to the scope chain, so it must be a
"method call" in some sense. And if - test('2'); - is a method call
then how can - test1('1'); - not be considered to be one? (...)
I agree that the way you've written it, yes, it might seem a bit
confusing, but clean it up a little bit :

(function () {
var o= {}, f;

f= o.f= function () { alert(this === o) };

f(); // false
o.f(); // true
with (o) { f() } // true
})();

and then it's quite evident (easy to figure out if you want) that
"with (o) { f() }" is "o.f()" in disguise.
(...) The
distinction certainly cannot be observed from the CallExpressions
themselves.
ISTM that if you know what you're looking for, or what to look for,
yes.
ISTM that even more so if you follow the advice to never use "with" (I
do).

Can you think of another situation/example where it's not easy to tell
whether you're calling a method or not ?

Thanks,
--
Jorge.
Sep 6 '08 #16
On Sep 4, 5:03*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
No, this would mean that the Global Object was not an object.
No, this means that calling f() is not necessarily the same as calling
window.f(), but 'this' will point to 'window' in both cases (except
sometimes within a 'with', as Henry has wisely pointed out).
It could also mean that there were instances where `this' was not preset
(...)
Because one could read your statement-question as "The value of 'this'
is preset whenever [something applies] *** except when/if the
function is called as an object's method ***".
1.- The value of 'this' is preset whenever a function is entered,
and,
2.- 'this' is always preset to the global object *** except when/if
the
function is called as an object's method *** ?

HTH,
--
Jorge.
Sep 6 '08 #17
Jorge wrote:
Thomas 'PointedEars' Lahn wrote:
>No, this would mean that the Global Object was not an object.

No, this means that calling f() is not necessarily the same as calling
window.f(), but 'this' will point to 'window' in both cases (except
sometimes within a 'with', as Henry has wisely pointed out).
Nonsense. What "Henry" pointed out instead was that without an identifier
as base of the reference the scope chain matters, especially if there is no
property accessor syntax at all. The `with' statement is but an example of
that, as it adds the object referred to by the statement's expression
parameter to the front of the scope chain for the following statement.
>It could also mean that there were instances where `this' was not preset
(...)
Because one could read your statement-question as "The value of 'this'
is preset whenever [something applies] *** except when/if the
function is called as an object's method ***".

1.- The value of 'this' is preset whenever a function is entered,
If you say "a function *execution context*", this is correct.
and,
2.- 'this' is always preset to the global object *** except when/if
the function is called as an object's method *** ?
Incorrect, because a function is *always* called as an object's method,
even though identifier resolution has to work along the scope chain, and
may only find a property with that name as one of the Global *Object*.
HTH

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>
Sep 8 '08 #18
Thomas 'PointedEars' Lahn wrote:
Jorge wrote:
<snip>
>and,
2.- 'this' is always preset to the global object *** except
when/if the function is called as an object's method *** ?

Incorrect, because a function is *always* called as an
object's method, ...
<snip>

It would be practical to assert that when, for example, a function
expression is executed directly or a function (even a method) is
returned from a function call and executed directly (return statements
uses GetValue on whatever they return so Reference types become
vales), (i.e. when the left of the 'call operators' evaluate to a
function reference value rather than a Reference type) then that
function is not "called as an object's method", though with the
defaulted - this - value it would still be possible to assert that it
was 'executed as a method' (of the global object).

Incidentally, there are plans to change the way - this - behaves in ES
3.1 (the next planned spec version, as ES 4 is on the backburner
again) such that - this - may actually be null or undefined in some
circumstances.
Sep 8 '08 #19
On Sep 8, 1:04*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>
Nonsense. *What "Henry" pointed out instead was that without an identifier
as base of the reference the scope chain matters, especially if there is no
property accessor syntax at all. *The `with' statement is but an example of
that
I can't figure out another case in which a call to f() would preset
'this' to something !== window ?

Could you show me how, when ?

(Not using 'with', .call() nor .apply())
It could also mean that there were instances where `this' was not preset
(...)
Because one could read your statement-question as "The value of 'this'
is preset whenever [something applies] *** except when/if the
function is called as an object's method ***".
1.- The value of 'this' is preset whenever a function is entered,

If you say "a function *execution context*", this is correct.
and,
2.- 'this' is always preset to the global object *** except when/if
the function is called as an object's method *** ?

Incorrect, because a function is *always* called as an object's method,
even though identifier resolution has to work along the scope chain, and
may only find a property with that name as one of the Global *Object*.
Let's say that f= object.method= function () { ... }

Called as an object's method : object.method()
Called as a function : f()

HTH,
--
Jorge.
Sep 8 '08 #20
On Sep 8, 1:43*pm, Henry <rcornf...@raindrop.co.ukwrote:
>
Incidentally, there are plans to change the way - this - behaves in ES
3.1 (the next planned spec version, as ES 4 is on the backburner
again) such that - this - may actually be null or undefined in some
circumstances.
Yes. 'this' shouldn't point to the global object unless f() ===
window.f(), isn't it ?
And 'this' for inner functions should probably better point to the
'outer' 'this'...

What other 'circumstances' are being devised ?

--
Jorge.
Sep 8 '08 #21
Jorge wrote:
Thomas 'PointedEars' Lahn wrote:
>Nonsense. What "Henry" pointed out instead was that without an identifier
as base of the reference the scope chain matters, especially if there is no
property accessor syntax at all. The `with' statement is but an example of
that

I can't figure out another case in which a call to f() would preset
'this' to something !== window ?
I do not know what you can figure out.

However, the (strict) equals operation is pointless as we are dealing with a
host object here. Therefore, you cannot reliably determine whether it is,
or is not, the same object. Not this way or any other way. Some want to
believe that there is proof they are the same, though.
Could you show me how, when ?

(Not using 'with', .call() nor .apply())
You missed the point, again.
>>2.- 'this' is always preset to the global object *** except when/if
the function is called as an object's method *** ?
Incorrect, because a function is *always* called as an object's method,
even though identifier resolution has to work along the scope chain, and
may only find a property with that name as one of the Global *Object*.

Let's say that f= object.method= function () { ... }

Called as an object's method : object.method()
Called as a function : f()
However, that is merely your amateurish interpretation, not what actually
happens. Which I am getting tired pointing out to you. Read the Spec.
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>
Sep 8 '08 #22
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
Jorge wrote:
>2.- 'this' is always preset to the global object *** except when/if
the function is called as an object's method *** ?

Incorrect, because a function is *always* called as an object's method,
even though identifier resolution has to work along the scope chain, and
may only find a property with that name as one of the Global *Object*.
That's one view, but no more correct than the opposing one: that the
"this" operator always evaluates to a value in a function code's
execution context, even when the function is *not* called as a method.

The closest the specification comes to defining "a method" is:
"A function stored in a property of an object is called a method."
(§ 4.3.3).

In the example:
function f(){return this;}
alert(f());
the function "f" is never a method according to this definition,
so it isn't called as a method.

There is no doubt what happens when you call a function. All we can
argue about is what to call it. :)
/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 8 '08 #23
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
>Jorge wrote:
>>2.- 'this' is always preset to the global object *** except when/if
the function is called as an object's method *** ?
Incorrect, because a function is *always* called as an object's method,
even though identifier resolution has to work along the scope chain, and
may only find a property with that name as one of the Global *Object*.

That's one view, but no more correct than the opposing one: that the
"this" operator always evaluates to a value in a function code's
execution context, even when the function is *not* called as a method.
Again, a function is always a method (there may be one exception: an
anonymous function immediately called after evaluation of its function
expression). Therefore, there is no case when it is not called as a method.
The closest the specification comes to defining "a method" is:
"A function stored in a property of an object is called a method."
(§ 4.3.3).
Exactly.
In the example:
function f(){return this;}
alert(f());
the function "f" is never a method according to this definition,
so it isn't called as a method.
You are mistaken. `f' is a property of the Variable Object of the execution
context; an object inaccessible by code when it is function code, but an
object nevertheless. That object is in the scope chain.
There is no doubt what happens when you call a function. All we can
argue about is what to call it. :)
*g*
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Sep 8 '08 #24
On Sep 8, 1:27 pm, Jorge wrote:
On Sep 8, 1:04 pm, Thomas 'PointedEars' Lahn wrote:
<snip>
I can't figure out another case in which a call to f()
would preset 'this' to something !== window ?

Could you show me how, when ?

(Not using 'with', .call() nor .apply())
<snip>

Disregarding quibbling about whether - window - is equivalent to the
ECMAScript global object (and there is plenty of room for quibbling in
that direction), the specification allows another situation in which a
non-property accessor to the left of call operators may allow a - this
- that was not the global object. Host functions/object methods are
explicitly allowed to return a Reference type if they want to. In the
even that a host function did return a Reference type that had a non-
activation object as its - base - and its property name referred to a
function reference value, and the call operators were directly applied
to the value returned from the call then in that second function call
the - this - keyword would refer the - base - of the Reference type
(assuming it was no an Activation object).

I am not aware of a single host method/function that behaves in this
way, but that does not mean that they do not exist, and the
specification allows for the possibility of their existence.

The form of this construct (if it exists) would be:-

HostObject.methodName()();

- where the first set of parenthesise call the method and the second
set call the return value from the first function call, which is
allowed to be a Reference type for host functions/methods.

IE host objects support this form of usage. If, for example, you had a
number of forms each with a field named 'f' IE will happily execute:-

document.forms(0)('f');

- and the call to the return value from the - docuemnt.forms(0) -
knows which of the forms it is associated with, such that the
subsequent ('f') returns the reference to the field in the correct
form. This could be achieved by - docuemnt.forms(0) - returning a
Reference type, but in practice it is not (other manipulations of the
construct can be used to demonstrate that returning a Reference type
is not what is happening here).
Sep 9 '08 #25
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
Lasse Reichstein Nielsen wrote:
>Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
>>Incorrect, because a function is *always* called as an object's method,
even though identifier resolution has to work along the scope chain, and
may only find a property with that name as one of the Global *Object*.
....[example that didn't fly]...
You are mistaken. `f' is a property of the Variable Object of the execution
context; an object inaccessible by code when it is function code, but an
object nevertheless. That object is in the scope chain.
Ok, I accept that.
But then:
var x = (function(){ return this; })();
is calling a function that isn't a property of any object, i.e., isn't
"called as a method" in any sense.

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 10 '08 #26
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
>Lasse Reichstein Nielsen wrote:
>>Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
Incorrect, because a function is *always* called as an object's method,
even though identifier resolution has to work along the scope chain, and
may only find a property with that name as one of the Global *Object*.

...[example that didn't fly]...
>You are mistaken. `f' is a property of the Variable Object of the execution
context; an object inaccessible by code when it is function code, but an
object nevertheless. That object is in the scope chain.

Ok, I accept that.
But then:
var x = (function(){ return this; })();
is calling a function that isn't a property of any object, i.e., isn't
"called as a method" in any sense.
First, that is *not* what Jorge was using to explain his meaning.

Second, you are pretty much preaching to the choir:

,-<48**************@PointedEars.de>
|
| Again, a function is always a method (there may be one exception: an
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| anonymous function immediately called after evaluation of its function
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
| expression). Therefore, there is no case when it is not called as a
^^^^^^^^^^^
| method.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Sep 10 '08 #27

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: BCC | last post by:
Hi, I have a class with several member variables that should be initialized according to user input before an object is instantiated. Using a static variable is required here. But, I would...
6
by: Nick Dreyer | last post by:
In VB.NET I would like to not have to create property get/set procedures for every class variable I want to expose to Excel VBA projects in COM builds. Can anyone tell me if that is possible, or...
3
by: | last post by:
I am trying to compile a simple c# class in Web Matrix called howdy.cs: // Program start class public class HowdyPartner { // Main begins program execution public static void Main() { //...
5
by: Logickle | last post by:
Hi, all. I'm working on an application which requires communicating session info between separate web apps running on the same web server. The out of process server method sounded ideal, and...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
8
by: dwok | last post by:
I have been wondering this for a while now. Suppose I have a class that contains some private member variables. How should I access the variables throughout the class? Should I use properties that...
27
by: thomasp | last post by:
Variables that I would like to make available to all forms and modules in my program, where should I declare them? At the momment I just created a module and have them all declared public there. ...
12
by: jimfortune | last post by:
I have a question based somewhat on: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/ddde992b84f762bd/152bbc027bf00720?hl=en#152bbc027bf00720 A local table works well...
19
by: pinkfloydhomer | last post by:
I can understand why properties are neat if you want to limit access (only get, no set), or you want to do some bookkeeping or sanity checking on values (in set) or if you want to change the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.