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


Groups > comp.lang.javascript > #23596

Re: Declaring global variables

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Declaring global variables
Date 2014-03-13 23:12 +0000
Organization A noiseless patient Spider
Message-ID <lfte0n$9ne$1@dont-email.me> (permalink)
References <6ae3i9h0e63li3380f0s84pi8h6uque0t3@4ax.com> <property-20140313151743@ram.dialup.fu-berlin.de> <mjg3i91m5etkcst8p1geghmdgmtnkhoas7@4ax.com> <lft04l$d9$1@speranza.aioe.org> <ap84i916l3jdtbbc1bnrde5po8sgdq8esa@4ax.com>

Show all headers | View raw


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

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web