simple variables as properties?

This is a discussion on simple variables as properties? within the Javascript forums in Programming Languages category; Jorge wrote: > 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 ...

Go Back   Application Development Forum > Programming Languages > Javascript

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
Reply

 

LinkBack Thread Tools Display Modes
  #11  
Old 09-04-2008, 08:40 AM
Thomas 'PointedEars' Lahn
Guest
 
Default Re: simple variables as properties?

Jorge wrote:
> 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>
Reply With Quote
  #12  
Old 09-04-2008, 09:28 AM
Jorge
Guest
 
Default Re: simple variables as properties?

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.
Reply With Quote
  #13  
Old 09-04-2008, 10:12 AM
Henry
Guest
 
Default Re: simple variables as properties?

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 -.
Reply With Quote
  #14  
Old 09-04-2008, 11:03 AM
Thomas 'PointedEars' Lahn
Guest
 
Default Re: simple variables as properties?

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>
Reply With Quote
  #15  
Old 09-04-2008, 03:54 PM
Dr J R Stockton
Guest
 
Default Re: simple variables as properties?

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

>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
  #16  
Old 09-06-2008, 12:05 PM
Jorge
Guest
 
Default Re: simple variables as properties?

On Sep 4, 4:12*pm, Henry <rcornf...@raindrop.co.uk> wrote:

> (...) 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.
Reply With Quote
  #17  
Old 09-06-2008, 12:42 PM
Jorge
Guest
 
Default Re: simple variables as properties?

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.
Reply With Quote
  #18  
Old 09-08-2008, 07:04 AM
Thomas 'PointedEars' Lahn
Guest
 
Default Re: simple variables as properties?

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>
Reply With Quote
  #19  
Old 09-08-2008, 07:43 AM
Henry
Guest
 
Default Re: simple variables as properties?

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.
Reply With Quote
  #20  
Old 09-08-2008, 08:27 AM
Jorge
Guest
 
Default Re: simple variables as properties?

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.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 05:44 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.