Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #23588 > unrolled thread

Declaring global variables

Started by"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
First post2014-03-13 07:04 -0700
Last post2014-03-14 07:03 -0300
Articles 10 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  Declaring global variables "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2014-03-13 07:04 -0700
    Re: Declaring global variables "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2014-03-13 07:39 -0700
      Re: Declaring global variables Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-03-13 16:15 -0300
        Re: Declaring global variables "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2014-03-13 14:30 -0700
          Re: Declaring global variables Denis McMahon <denismfmcmahon@gmail.com> - 2014-03-13 23:12 +0000
            Re: Declaring global variables "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2014-03-13 16:53 -0700
    Re: Declaring global variables Martin Honnen <mahotrash@yahoo.de> - 2014-03-13 16:15 +0100
    Re: Declaring global variables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-14 01:08 +0100
      Re: Declaring global variables JJ <duh@nah.meh> - 2014-03-14 09:46 +0700
        Re: Declaring global variables Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-03-14 07:03 -0300

#23588 — Declaring global variables

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2014-03-13 07:04 -0700
SubjectDeclaring global variables
Message-ID<6ae3i9h0e63li3380f0s84pi8h6uque0t3@4ax.com>
Within a function I want to declare some variables as global. I do this
with 'window.variable = value;' 

This seems to work, but adding the 'var' prefix throws an error, i.e., 
'var window.variable = value' throws "Syntax error while loading: expected
';', got '.'"

Why?
-- 
Joey

[toc] | [next] | [standalone]


#23590

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2014-03-13 07:39 -0700
Message-ID<mjg3i91m5etkcst8p1geghmdgmtnkhoas7@4ax.com>
In reply to#23588
Stefan Ram wrote:

>"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>>This seems to work, but adding the 'var' prefix throws an error, i.e., 
>>'var window.variable = value' throws "Syntax error while loading: expected
>>';', got '.'"
>
>  »var« declares a (new) /variable/.
>
>  »window.variable =« adds a /property/ to the window object,
>  or sets the value of a window property.

Thanks. I'm confused, as I am declaring a new variable. Don't know why
it's throwing the error.

Am I correct in presuming that 'window.variable = value' within a function
is creating a global 'variable = value?'
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#23592

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2014-03-13 16:15 -0300
Message-ID<lft04l$d9$1@speranza.aioe.org>
In reply to#23590
Em 13/03/2014 11:39, Joey@still_Learning.invalid escreveu:
> Stefan Ram wrote:
>
>> "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>>> This seems to work, but adding the 'var' prefix throws an error, i.e.,
>>> 'var window.variable = value' throws "Syntax error while loading: expected
>>> ';', got '.'"
>>
>>   »var« declares a (new) /variable/.
>>
>>   »window.variable =« adds a /property/ to the window object,
>>   or sets the value of a window property.
>
> Thanks. I'm confused, as I am declaring a new variable. Don't know why
> it's throwing the error.

You cannot define a variable in that manner (var window.variable = 
whatever). I'd suggest this read:

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals>

>
> Am I correct in presuming that 'window.variable = value' within a function
> is creating a global 'variable = value?'
>

According to the above article, "Global variables are in fact properties 
of the global object. In web pages the global object is window, so you 
can set and access global variables using the window.variable syntax."

-- 
Joao Rodrigues (J.R.)

[toc] | [prev] | [next] | [standalone]


#23595

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2014-03-13 14:30 -0700
Message-ID<ap84i916l3jdtbbc1bnrde5po8sgdq8esa@4ax.com>
In reply to#23592
Joao Rodrigues wrote:

>Em 13/03/2014 11:39, Joey@still_Learning.invalid escreveu:
>> Stefan Ram wrote:
>>
>>> "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>>>> This seems to work, but adding the 'var' prefix throws an error, i.e.,
>>>> 'var window.variable = value' throws "Syntax error while loading: expected
>>>> ';', got '.'"
>>>
>>>   »var« declares a (new) /variable/.
>>>
>>>   »window.variable =« adds a /property/ to the window object,
>>>   or sets the value of a window property.
>>
>> Thanks. I'm confused, as I am declaring a new variable. Don't know why
>> it's throwing the error.
>
>You cannot define a variable in that manner (var window.variable = 
>whatever). I'd suggest this read:
>
><https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals>
>
>>
>> Am I correct in presuming that 'window.variable = value' within a function
>> is creating a global 'variable = value?'
>>
>
>According to the above article, "Global variables are in fact properties 
>of the global object. In web pages the global object is window, so you 
>can set and access global variables using the window.variable syntax."

Is this not what I'm doing?
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#23596

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-03-13 23:12 +0000
Message-ID<lfte0n$9ne$1@dont-email.me>
In reply to#23595
On Thu, 13 Mar 2014 14:30:44 -0700, Joey@still_Learning.invalid wrote:

> Joao Rodrigues wrote:
> 
>>Em 13/03/2014 11:39, Joey@still_Learning.invalid escreveu:
>>> Stefan Ram wrote:
>>>
>>>> "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>>>>> This seems to work, but adding the 'var' prefix throws an error,
>>>>> i.e., 'var window.variable = value' throws "Syntax error while
>>>>> loading: expected ';', got '.'"
>>>>
>>>>   »var« declares a (new) /variable/.
>>>>
>>>>   »window.variable =« adds a /property/ to the window object,
>>>>   or sets the value of a window property.
>>>
>>> Thanks. I'm confused, as I am declaring a new variable. Don't know why
>>> it's throwing the error.
>>
>>You cannot define a variable in that manner (var window.variable =
>>whatever). I'd suggest this read:
>>
>><https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/
Values,_variables,_and_literals>
>>
>>
>>> Am I correct in presuming that 'window.variable = value' within a
>>> function is creating a global 'variable = value?'
>>>
>>>
>>According to the above article, "Global variables are in fact properties
>>of the global object. In web pages the global object is window, so you
>>can set and access global variables using the window.variable syntax."
> 
> Is this not what I'm doing?

Simple answer: No.

The important stuff to know:

You can use

window.new_property = x;

Or you can use:

var new_property;
new_property = x;

Or even:

var new_property = x;

Or even just:

new_property = x;

But you can not use:

var window.new_property = x;

The long and complex answer will probably melt your brain, and you 
probably don't actually need to know it, but if you insist, be the 
masochist that asks for it loudly enough and someone will give it to you.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#23597

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2014-03-13 16:53 -0700
Message-ID<s5h4i9tl5o4o0f8vj015nukkl142chb37b@4ax.com>
In reply to#23596
Denis McMahon wrote:

>On Thu, 13 Mar 2014 14:30:44 -0700, Joey@still_Learning.invalid wrote:
>
>> Joao Rodrigues wrote:
>> 
>>>Em 13/03/2014 11:39, Joey@still_Learning.invalid escreveu:
>>>> Stefan Ram wrote:
>>>>
>>>>> "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>>>>>> This seems to work, but adding the 'var' prefix throws an error,
>>>>>> i.e., 'var window.variable = value' throws "Syntax error while
>>>>>> loading: expected ';', got '.'"
>>>>>
>>>>>   »var« declares a (new) /variable/.
>>>>>
>>>>>   »window.variable =« adds a /property/ to the window object,
>>>>>   or sets the value of a window property.
>>>>
>>>> Thanks. I'm confused, as I am declaring a new variable. Don't know why
>>>> it's throwing the error.
>>>
>>>You cannot define a variable in that manner (var window.variable =
>>>whatever). I'd suggest this read:
>>>
>>><https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/
>Values,_variables,_and_literals>
>>>
>>>
>>>> Am I correct in presuming that 'window.variable = value' within a
>>>> function is creating a global 'variable = value?'
>>>>
>>>>
>>>According to the above article, "Global variables are in fact properties
>>>of the global object. In web pages the global object is window, so you
>>>can set and access global variables using the window.variable syntax."
>> 
>> Is this not what I'm doing?
>
>Simple answer: No.
>
>The important stuff to know:
>
>You can use
>
>window.new_property = x;
>
>Or you can use:
>
>var new_property;
>new_property = x;
>
>Or even:
>
>var new_property = x;
>
>Or even just:
>
>new_property = x;
>
>But you can not use:
>
>var window.new_property = x;
>
>The long and complex answer will probably melt your brain, and you 
>probably don't actually need to know it, but if you insist, be the 
>masochist that asks for it loudly enough and someone will give it to you.

Got it! Thanks.

(And, my brain melted years ago.)
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#23591

FromMartin Honnen <mahotrash@yahoo.de>
Date2014-03-13 16:15 +0100
Message-ID<5321cb77$0$9525$9b4e6d93@newsspool1.arcor-online.net>
In reply to#23588
Joey@still_Learning.invalid wrote:
> Within a function I want to declare some variables as global. I do this
> with 'window.variable = value;'
>
> This seems to work, but adding the 'var' prefix throws an error, i.e.,
> 'var window.variable = value' throws "Syntax error while loading: expected
> ';', got '.'"
>
> Why?

The only way to declare a global variable is with

var varName;

outside of any function code. If you want to initialize it at the same 
time you use

var varName = expression;

[toc] | [prev] | [next] | [standalone]


#23598

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-03-14 01:08 +0100
Message-ID<139484623.PyJ8SnIKrL@PointedEars.de>
In reply to#23588
Stefan Ram wrote:

> "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>> This seems to work, but adding the 'var' prefix throws an error, i.e.,
>> 'var window.variable = value' throws "Syntax error while loading:
>> expected ';', got '.'"
> 
>   »var« declares a (new) /variable/.
> 
>   »window.variable =« adds a /property/ to the window object,
>   or sets the value of a window property.

A very important difference indeed.  Many newcomers think the “window” 
property is a built-in identical to the global object when it is in fact a 
host-defined context-dependent property referring to a host object that 
happens to be in the scope chain in that host environment for historical 
reasons.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#23599

FromJJ <duh@nah.meh>
Date2014-03-14 09:46 +0700
Message-ID<wssgli3g4uk8$.oosslpxboc6z$.dlg@40tude.net>
In reply to#23598
On Fri, 14 Mar 2014 01:08:25 +0100, Thomas 'PointedEars' Lahn wrote:
> 
> A very important difference indeed.  Many newcomers think the “window” 
> property is a built-in identical to the global object when it is in fact a 
> host-defined context-dependent property referring to a host object that 
> happens to be in the scope chain in that host environment for historical 
> reasons.

I get the feeling that declaring a global variable, somehow behind the
scene, became adding a property to the window object.

e.g. global:

var abc=1;
console.log("abc" in window); //true

Whereas for local context, inside other object...

var obj={
  fn:function(){
   var abc=1;
   console.log("abc" in this); //false
  }
};
obj.fn();

I assume the above behaviour was done in compliance with the DOM specs?

[toc] | [prev] | [next] | [standalone]


#23602

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2014-03-14 07:03 -0300
Message-ID<lfuk56$jju$1@speranza.aioe.org>
In reply to#23599
On 13-03-2014 23:46, JJ wrote:
> On Fri, 14 Mar 2014 01:08:25 +0100, Thomas 'PointedEars' Lahn wrote:
>>
>> A very important difference indeed.  Many newcomers think the “window”
>> property is a built-in identical to the global object when it is in fact a
>> host-defined context-dependent property referring to a host object that
>> happens to be in the scope chain in that host environment for historical
>> reasons.
>
> I get the feeling that declaring a global variable, somehow behind the
> scene, became adding a property to the window object.
>
> e.g. global:
>
> var abc=1;
> console.log("abc" in window); //true
>
> Whereas for local context, inside other object...
>
> var obj={
>    fn:function(){
>     var abc=1;
>     console.log("abc" in this); //false
>    }
> };
> obj.fn();

This behaviour is explained in the following MDN article:

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals>

Global variables are in fact properties of the global object.

>
> I assume the above behaviour was done in compliance with the DOM specs?
>

I'll pass on that.

-- 
Joao Rodrigues

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web