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


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

variable access

Started bysaurabh.khemka4@gmail.com
First post2013-05-05 03:19 -0700
Last post2013-05-06 01:09 +0200
Articles 8 — 5 participants

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


Contents

  variable access saurabh.khemka4@gmail.com - 2013-05-05 03:19 -0700
    Re: variable access Martin Honnen <mahotrash@yahoo.de> - 2013-05-05 13:12 +0200
      Re: variable access saurabh.khemka4@gmail.com - 2013-05-05 05:48 -0700
        Re: variable access saurabh.khemka4@gmail.com - 2013-05-05 07:44 -0700
          Re: variable access "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-05-05 17:30 +0200
        Re: variable access Stanimir Stamenkov <s7an10@netscape.net> - 2013-05-05 19:39 +0300
        Re: variable access Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-05 19:47 +0200
          Re: variable access Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-06 01:09 +0200

#19636 — variable access

Fromsaurabh.khemka4@gmail.com
Date2013-05-05 03:19 -0700
Subjectvariable access
Message-ID<90af5a5a-ba92-4745-917c-a8ad3ff54704@googlegroups.com>
I have a javascript file where I am assigning some value to a variable. How do I access the same variable in some other javascript file? Please suggest.

[toc] | [next] | [standalone]


#19637

FromMartin Honnen <mahotrash@yahoo.de>
Date2013-05-05 13:12 +0200
Message-ID<51863ea9$0$6640$9b4e6d93@newsspool2.arcor-online.net>
In reply to#19636
saurabh.khemka4@gmail.com wrote:
> I have a javascript file where I am assigning some value to a variable. How do I access the same variable in some other javascript file? Please suggest.

Inside of HTML documents in a browser? Simply include the files

<script src="file1.js"></script>
<script src="file2.js"></script>

Then file2.js can access global variables defined in file1.js.

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


#19638

Fromsaurabh.khemka4@gmail.com
Date2013-05-05 05:48 -0700
Message-ID<c56b3a60-c211-4bf5-8e67-fdf1789fb5e8@googlegroups.com>
In reply to#19637
On Sunday, 5 May 2013 04:12:41 UTC-7, Martin Honnen  wrote:
> saurabh.khemka4@gmail.com wrote:
> 
> > I have a javascript file where I am assigning some value to a variable. How do I access the same variable in some other javascript file? Please suggest.
> 
> 
> 
> Inside of HTML documents in a browser? Simply include the files
> 
> 
> 
> <script src="file1.js"></script>
> 
> <script src="file2.js"></script>
> 
> 
> 
> Then file2.js can access global variables defined in file1.js.

Suppose I have a variable declared in javascript file(1.js)

game = "abc";

Inside that javascript, a method is called which changes its value to xyz.

Now, when I try to access that variable game in another javascript file(2.js),

value abc is only returned. Please suggest.

In the html page of 2.js, Both the javascript files are included. First 1.js and below that 2.js.

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


#19639

Fromsaurabh.khemka4@gmail.com
Date2013-05-05 07:44 -0700
Message-ID<8457fe97-56d8-4541-b43c-269e984422f3@googlegroups.com>
In reply to#19638
On Sunday, 5 May 2013 05:48:36 UTC-7, saurabh...@gmail.com  wrote:
> On Sunday, 5 May 2013 04:12:41 UTC-7, Martin Honnen  wrote:
> 
> > saurabh.khemka4@gmail.com wrote:
> 
> > 
> 
> > > I have a javascript file where I am assigning some value to a variable. How do I access the same variable in some other javascript file? Please suggest.
> 
> > 
> 
> > 
> 
> > 
> 
> > Inside of HTML documents in a browser? Simply include the files
> 
> > 
> 
> > 
> 
> > 
> 
> > <script src="file1.js"></script>
> 
> > 
> 
> > <script src="file2.js"></script>
> 
> > 
> 
> > 
> 
> > 
> 
> > Then file2.js can access global variables defined in file1.js.
> 
> 
> 
> Suppose I have a variable declared in javascript file(1.js)
> 
> 
> 
> game = "abc";
> 
> 
> 
> Inside that javascript, a method is called which changes its value to xyz.
> 
> 
> 
> Now, when I try to access that variable game in another javascript file(2.js),
> 
> 
> 
> value abc is only returned. Please suggest.
> 
> 
> 
> In the html page of 2.js, Both the javascript files are included. First 1.js and below that 2.js.

Also, if that variable is an object, then how do I pass that object?

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


#19640

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-05-05 17:30 +0200
Message-ID<XnsA1B7B2212AF31eejj99@194.109.133.133>
In reply to#19639
 wrote on 05 mei 2013 in comp.lang.javascript:

>> In the html page of 2.js, Both the javascript files are included.
>> First 1.js and below that 2.js. 
> 
> Also, if that variable is an object, then how do I pass that object?

Objects and variables do not exist in a .js file,
the file is just a text to be executed.

When different .js files are executed in the same execution environment,
they act just as the execution of one single file.

So there is no question about passing an object or variable,
they exist in that one environment.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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


#19642

FromStanimir Stamenkov <s7an10@netscape.net>
Date2013-05-05 19:39 +0300
Message-ID<km61q3$75c$1@dont-email.me>
In reply to#19638
Sun, 5 May 2013 05:48:36 -0700 (PDT), /saurabh.khemka4@gmail.com/:
> On Sunday, 5 May 2013 04:12:41 UTC-7, Martin Honnen wrote:
>> saurabh.khemka4@gmail.com wrote:
>>
>>> I have a javascript file where I am assigning some value to a
>>> variable. How do I access the same variable in some other
>>> javascript file? Please suggest.
>>
>> Inside of HTML documents in a browser? Simply include the
>> files
>>
>> <script src="file1.js"></script>
>> <script src="file2.js"></script>
>>
>> Then file2.js can access global variables defined in file1.js.
>
> Suppose I have a variable declared in javascript file(1.js)
>
> game = "abc";
>
> Inside that javascript, a method is called which changes its
> value to xyz.
>
> Now, when I try to access that variable game in another
> javascript file(2.js),
>
> value abc is only returned. Please suggest.

Are you sure the method from the first file modifying the variable 
value is called (if at all) before you access it from code in the 
second file?  To verify, place a statement invoking the given method 
at the end of the first file, like:

var game = "abc";
...
function myMethod() {
   game = "xyz";
}
...
myMethod();

> In the html page of 2.js, Both the javascript files are included.
> First 1.js and below that 2.js.

-- 
Stanimir

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


#19643

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-05 19:47 +0200
Message-ID<5148182.RP0mX05cp1@PointedEars.de>
In reply to#19638
saurabh.khemka4@gmail.com wrote:

> On Sunday, 5 May 2013 04:12:41 UTC-7, Martin Honnen  wrote:
>> saurabh.khemka4@gmail.com wrote:
>> > I have a javascript file where I am assigning some value to a variable.
>> > How do I access the same variable in some other javascript file? Please
>> > suggest.
>> 
>> Inside of HTML documents in a browser? Simply include the files
>> 
>> <script src="file1.js"></script>
>> <script src="file2.js"></script>
>> 
>> Then file2.js can access global variables defined in file1.js.
> 
> Suppose I have a variable declared in javascript file(1.js)
> 
> game = "abc";

This is _not_ a variable declaration.  A variable declaration starts with

  var

followed by the variable identifier, and an optional initialization part.  

You are attempting to add a property named “game” to an object, which, by 
contrast to a variable declaration, can fail even if syntactically correct.  

For example, if there is an element with name or ID “abc” in your document, 
and you are using IE/MSHTML, this assignment *will* fail because the global 
object or another object before it in the scope chain already has a non-
writable property of that name that refers to the corresponding object.
 
> Inside that javascript,

There is no javascript.  <http://PointedEars.de/es-matrix>

> a method is called which changes its value to xyz.

OK.

> Now, when I try to access that variable game in another javascript
> file(2.js),

Client-side script resources never stand alone.  There is always a runtime 
environment that uses them.  Usually the runtime environment is a Web 
browser, and separate files are used in HTML with the “script” element.  
Where *exactly* are your “script” elements?
 
> value abc is only returned.

Values are returned by functions.  Where is your function?

> Please suggest.

Please RTFM and STFW.  This is neither Programming 101 nor a guessing game.

> In the html page of 2.js,

That is “_HTML document_ that includes the script resource 2.js”.

> Both the javascript files are included. First
> 1.js and below that 2.js.

So, assuming the simple assignment in 1.js worked at all, you have to find 
out which code in 2.js changes the value defined in 1.js.  It is impossible 
to say without your posting the relevant code.  Use a debugger.

Please do not use the borken Google Groups for posting; use a newsreader.  
And please get a real name.

<http://jibbering.com/faq/#posting>
<http://PointedEars.de/faq#posting>

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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


#19644

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-06 01:09 +0200
Message-ID<1629412.iLo1Md9sQa@PointedEars.de>
In reply to#19643
Thomas 'PointedEars' Lahn wrote:

> saurabh.khemka4@gmail.com wrote:
>> Suppose I have a variable declared in javascript file(1.js)
>> 
>> game = "abc";
> 
> This is _not_ a variable declaration.  A variable declaration starts with
> 
>   var
> 
> followed by the variable identifier, and an optional initialization part.
> 
> You are attempting to add a property named “game” to an object, which, by
> contrast to a variable declaration, can fail even if syntactically
> correct.
> 
> For example, if there is an element with name or ID “abc” in your
                                           ^^^^^^^^^^^^^^^^
I meant name or ID “game”, of course.  The left-hand side of the simple 
assignment specifies name of the property whose value may not be modifiable.

> document, and you are using IE/MSHTML, this assignment *will* fail because
> the global object or another object before it in the scope chain already
> has a non- writable property of that name that refers to the corresponding
> object.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


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


csiph-web