[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Declaring the same global variable multiple times

Joey@still_Learning.invalid

6/7/2015 3:14:00 PM

I have an extensive script that often generates the same global
variable in multiple functions. I declare each a global by defining as
window.variable = xxx.

Do multiple declarations require greater compiling or otherwise
overhead, i.e., performance degradation?

Instead of re-declaring should I declare 'var xxx' outside the
functions? Does this accomplish the same as above?

TIA,
--
Joey
10 Answers

Steven D'Aprano

6/7/2015 3:56:00 PM

0

On Mon, 8 Jun 2015 01:13 am, Joey@still_Learning.invalid wrote:

> I have an extensive script that often generates the same global
> variable in multiple functions. I declare each a global by defining as
> window.variable = xxx.
>
> Do multiple declarations require greater compiling or otherwise
> overhead, i.e., performance degradation?


Surely this is premature optimization? Unless the calculation of xxx is
horribly expensive, or you are performing it millions of times, I find it
hard to believe that this could be anything more than a micro-optimization
(shaving a few hundredth of a millisecond off the runtime or so).


> Instead of re-declaring should I declare 'var xxx' outside the
> functions? Does this accomplish the same as above?

It seems to me that an outer declaration should be preferred, not because it
is "more efficient to run", but because it is more readable, more
understandable, and more easily maintained. If you decide to change it from
variable = xxx to variable = yyy, you only need to change one place,
instead of twenty.


--
Steven

ram

6/7/2015 5:21:00 PM

0

"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>I have an extensive script that often generates the same global
>variable in multiple functions. I declare each a global by defining as
>window.variable = x
[last quoted line abbreviated]

A variable is declared with »var«/»let«/»const«.

»window.variable = alpha« is a /property accessor expression/.

Evaluating such an expression defines a new property of the
object »window« and sets its value to »alpha« if the
property name »variable« has not been defined at the
beginning of its evaluation, or otherwise it sets the
value of an existing property name »variable« of the
object »window« to »alpha«.

Denis McMahon

6/7/2015 9:14:00 PM

0

On Sun, 07 Jun 2015 08:13:50 -0700, Joey@still_Learning.invalid wrote:

> I have an extensive script that often generates the same global variable

Why are you using global variables? Unless you can answer this question
in a convincing manner, you probably shouldn't be using global variables.

--
Denis McMahon, denismfmcmahon@gmail.com

Joey@still_Learning.invalid

6/7/2015 9:23:00 PM

0

On Sun, 07 Jun 2015 08:13:50 -0700, "Joey@still_Learning.invalid"
<Joey@still_Learning.invalid> wrote:

>I have an extensive script that often generates the same global
>variable in multiple functions. I declare each a global by defining as
>window.variable = xxx.
>
>Do multiple declarations require greater compiling or otherwise
>overhead, i.e., performance degradation?
>
>Instead of re-declaring should I declare 'var xxx' outside the
>functions? Does this accomplish the same as above?
>
Thanks, Steven and Stefan. External it is.
--
Joey

Joey@still_Learning.invalid

6/8/2015 12:34:00 AM

0

On Sun, 7 Jun 2015 21:14:23 +0000 (UTC), Denis McMahon
<denismfmcmahon@gmail.com> wrote:

>On Sun, 07 Jun 2015 08:13:50 -0700, Joey@still_Learning.invalid wrote:
>
>> I have an extensive script that often generates the same global variable
>
>Why are you using global variables? Unless you can answer this question
>in a convincing manner, you probably shouldn't be using global variables.

I use the same variables with the same values in multiple functions.
Instead of recomputing in each function, I compute once to a global.
--
Joey

Cezary Tomczyk

6/8/2015 6:57:00 AM

0

On 2015-06-08 02:33, Joey@still_Learning.invalid wrote:
> On Sun, 7 Jun 2015 21:14:23 +0000 (UTC), Denis McMahon
> <denismfmcmahon@gmail.com> wrote:
>
>> On Sun, 07 Jun 2015 08:13:50 -0700, Joey@still_Learning.invalid wrote:
>>
>>> I have an extensive script that often generates the same global variable
>>
>> Why are you using global variables? Unless you can answer this question
>> in a convincing manner, you probably shouldn't be using global variables.
>
> I use the same variables with the same values in multiple functions.
> Instead of recomputing in each function, I compute once to a global.

Instead of that you may create singleton which can store your data in a
property and add method to get/set values.

Example:

var example = {

sharedVariable: undefined,

getValue : function () {
return this.sharedVariable
},

setValue : function (value) {
this.sharedVariable = value;
}

};

There are many other ways to avoid using global variables.

--
Cezary Tomczyk
http://www.ct...

Joey@still_Learning.invalid

6/8/2015 2:29:00 PM

0

On Mon, 08 Jun 2015 08:56:54 +0200, Cezary Tomczyk
<cezary.tomczyk@gmail.com> wrote:

>On 2015-06-08 02:33, Joey@still_Learning.invalid wrote:
>> On Sun, 7 Jun 2015 21:14:23 +0000 (UTC), Denis McMahon
>> <denismfmcmahon@gmail.com> wrote:
>>
>>> On Sun, 07 Jun 2015 08:13:50 -0700, Joey@still_Learning.invalid wrote:
>>>
>>>> I have an extensive script that often generates the same global variable
>>>
>>> Why are you using global variables? Unless you can answer this question
>>> in a convincing manner, you probably shouldn't be using global variables.
>>
>> I use the same variables with the same values in multiple functions.
>> Instead of recomputing in each function, I compute once to a global.
>
>Instead of that you may create singleton which can store your data in a
>property and add method to get/set values.
>
>Example:
>
>var example = {
>
> sharedVariable: undefined,
>
> getValue : function () {
> return this.sharedVariable
> },
>
> setValue : function (value) {
> this.sharedVariable = value;
> }
>
>};
>
>There are many other ways to avoid using global variables.

Thank you.
--
Joey

ram

6/8/2015 2:56:00 PM

0

Cezary Tomczyk <cezary.tomczyk@gmail.com> writes:
>Instead of that

(that = a global)

> you may create singleton which can store your data in a
>property and add method to get/set values.
>Example:
>var example = {
> sharedVariable: undefined,

And »example« is what? Not a global?

Christoph M. Becker

6/8/2015 3:03:00 PM

0

Stefan Ram wrote:

> Cezary Tomczyk <cezary.tomczyk@gmail.com> writes:
>> Instead of that
>
> (that = a global)
>
>> you may create singleton which can store your data in a
>> property and add method to get/set values.
>> Example:
>> var example = {
>> sharedVariable: undefined,
>
> And »example« is what? Not a global?

Basically, `example` is a global, but the namespace pattern helps to
*reduce* the number of globals. To *avoid* globals, IIFEs or the module
pattern might be more appropriate.

--
Christoph M. Becker

Cezary Tomczyk

6/8/2015 5:09:00 PM

0

On 2015-06-08 16:56, Stefan Ram wrote:
> Cezary Tomczyk <cezary.tomczyk@gmail.com> writes:
>> Instead of that
>
> (that = a global)
>
>> you may create singleton which can store your data in a
>> property and add method to get/set values.
>> Example:
>> var example = {
>> sharedVariable: undefined,
>
> And »example« is what? Not a global?

Yes, example is in global context. However, still better than defining
more than one global variable.

On the other hand I would encapsulate it with AMD pattern:

define(function(){

var example = {};

return example;

});

but that's different story.

--
Cezary Tomczyk
http://www.ct...