Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #28024 > unrolled thread
| Started by | "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> |
|---|---|
| First post | 2015-06-07 08:13 -0700 |
| Last post | 2015-06-07 14:23 -0700 |
| Articles | 9 — 5 participants |
Back to article view | Back to comp.lang.javascript
Declaring the same global variable multiple times "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2015-06-07 08:13 -0700
Re: Declaring the same global variable multiple times Steven D'Aprano <steve@pearwood.info> - 2015-06-08 01:56 +1000
Re: Declaring the same global variable multiple times Denis McMahon <denismfmcmahon@gmail.com> - 2015-06-07 21:14 +0000
Re: Declaring the same global variable multiple times "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2015-06-07 17:33 -0700
Re: Declaring the same global variable multiple times Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2015-06-08 08:56 +0200
Re: Declaring the same global variable multiple times "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2015-06-08 07:28 -0700
Re: Declaring the same global variable multiple times "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-06-08 17:03 +0200
Re: Declaring the same global variable multiple times Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2015-06-08 19:09 +0200
Re: Declaring the same global variable multiple times "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2015-06-07 14:23 -0700
| From | "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> |
|---|---|
| Date | 2015-06-07 08:13 -0700 |
| Subject | Declaring the same global variable multiple times |
| Message-ID | <7kn8natinqhrjpa3luqivrlcbrkb2mule3@4ax.com> |
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
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-06-08 01:56 +1000 |
| Message-ID | <557469a5$0$12982$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #28024 |
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
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-06-07 21:14 +0000 |
| Message-ID | <ml2c7f$an8$1@dont-email.me> |
| In reply to | #28024 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> |
|---|---|
| Date | 2015-06-07 17:33 -0700 |
| Message-ID | <tho9naln5nh9d8tugq0n0efpthccat9r10@4ax.com> |
| In reply to | #28034 |
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
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2015-06-08 08:56 +0200 |
| Message-ID | <38a42$55753cb7$6def49d5$28976@nntpswitch.blueworldhosting.com> |
| In reply to | #28038 |
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.ctomczyk.pl/
[toc] | [prev] | [next] | [standalone]
| From | "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> |
|---|---|
| Date | 2015-06-08 07:28 -0700 |
| Message-ID | <3k9bnale8aml5kib68g10ologlitsj3mmu@4ax.com> |
| In reply to | #28041 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Christoph M. Becker" <cmbecker69@arcor.de> |
|---|---|
| Date | 2015-06-08 17:03 +0200 |
| Message-ID | <ml4arq$il7$1@solani.org> |
| In reply to | #28041 |
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
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2015-06-08 19:09 +0200 |
| Message-ID | <6eed8$5575cc45$6def49d5$31491@nntpswitch.blueworldhosting.com> |
| In reply to | #28041 |
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.ctomczyk.pl/
[toc] | [prev] | [next] | [standalone]
| From | "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> |
|---|---|
| Date | 2015-06-07 14:23 -0700 |
| Message-ID | <3gd9na90q7tb6a9gj33365qck73i91q1br@4ax.com> |
| In reply to | #28024 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web