HighDots Forums  

simple variables as properties?

Javascript JavaScript language (comp.lang.javascript)


Discuss simple variables as properties? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
Jorge
 
Posts: n/a

Default Re: simple variables as properties? - 09-04-2008 , 09:28 AM






On Sep 4, 2:40*pm, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de>
wrote:
Quote:
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.


Reply With Quote
  #12  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: simple variables as properties? - 09-04-2008 , 11:03 AM






Jorge wrote:
Quote:
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>


Reply With Quote
  #13  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: simple variables as properties? - 09-04-2008 , 03:54 PM



In comp.lang.javascript message <c96a80e0-10d0-4d4f-9e6c-60139b328829@n3
8g2000prl.googlegroups.com>, Thu, 4 Sep 2008 03:34:08, Henry
<rcornford (AT) raindrop (DOT) co.uk> posted:

Quote:
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)


Reply With Quote
  #14  
Old   
Jorge
 
Posts: n/a

Default Re: simple variables as properties? - 09-06-2008 , 12:05 PM



On Sep 4, 4:12*pm, Henry <rcornf... (AT) raindrop (DOT) co.uk> wrote:

Quote:
(...) 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.

Quote:
(...) 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.


Reply With Quote
  #15  
Old   
Jorge
 
Posts: n/a

Default Re: simple variables as properties? - 09-06-2008 , 12:42 PM



On Sep 4, 5:03*pm, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de>
wrote:
Quote:
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).

Quote:
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.


Reply With Quote
  #16  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: simple variables as properties? - 09-08-2008 , 07:04 AM



Jorge wrote:
Quote:
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.

Quote:
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.

Quote:
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>


Reply With Quote
  #17  
Old   
Henry
 
Posts: n/a

Default Re: simple variables as properties? - 09-08-2008 , 07:43 AM



Thomas 'PointedEars' Lahn wrote:
Quote:
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.


Reply With Quote
  #18  
Old   
Jorge
 
Posts: n/a

Default Re: simple variables as properties? - 09-08-2008 , 08:27 AM



On Sep 8, 1:04*pm, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de>
wrote:
Quote:
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())

Quote:
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.


Reply With Quote
  #19  
Old   
Jorge
 
Posts: n/a

Default Re: simple variables as properties? - 09-08-2008 , 08:34 AM



On Sep 8, 1:43*pm, Henry <rcornf... (AT) raindrop (DOT) co.uk> wrote:
Quote:
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.


Reply With Quote
  #20  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: simple variables as properties? - 09-08-2008 , 10:17 AM



Jorge wrote:
Quote:
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.

Quote:
Could you show me how, when ?

(Not using 'with', .call() nor .apply())
You missed the point, again.

Quote:
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>


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.