tags:

views:

89

answers:

4

Hi all,

I prefer to declare one Javascript file for my all website. I am trying to decrease the usage of global variables. My examples at the below, in both case each object has a myName field.

  1. I would like to know when they are initialized?
  2. And In terms of memory and efficiency which one is more effective?
  3. For variable a, is declaring a.myName the same as global "var myName = Rebecca" ?
var a = {
   myName : 'Rebecca' ,

   sayHello : function() {
      console.log(this.myName);
   }
};

var b = {
   myName : function() {
      return 'Rebecca';
   },

   sayHello : function() {
      console.log(this.myName());
   }
};

Thanks

A: 

They will be initialized when the statements are first encountered. In a, 'Rebecca' is initialized as the value for the myName key. In b, it's just data internal to the myName (anonymous) function. a will be slightly more efficient because it avoids a function call. I also find it more readable in this simple example.

I find the choice to put everything in a single file questionable. In some cases, you want a modular design. And since you're worried about efficiency (albeit perhaps prematurely), note that having one big file can actually hurt performance if pages include code they don't need.

Matthew Flaschen
A: 

a doesn't make any sense, because you're logging a function reference. B is the way to go, since you're actually invoking the method, using ().

Evan Trimboli
A: 

1) They are initialized when the script is processed in the browser, unless you declare the objects in an event handler. In that case the object is created when the event script is executed.

2) In terms of efficiency, a will probably be more efficient. Note though that in the first case you use a.myName and in the second b.myName() to get the value of the property.

3) No. If you assign a value to a property of an object, you always have to get that value through the object. In this case either a.myName or a['myName'].

Marnix van Valen
a.myName vs. a.myName(): the former can be changed at any time while the latter will always return "Rebecca".
Dormilich
`a.MyName` can be changed too. `a.myName = function() { return "Deborah"; };`
Matthew Flaschen
A: 
  1. I believe these will be initialized identically (i.e. when the code is reached). What's different is what's happening when they are initialized and where the load is placed when their data is actually required.

  2. To me, it would depend a lot on what you were expecting to have in myName. If it were just a string, I'd avoid the function and go with choice a. On the other hand, if there were a great deal of logic involved and that logic might not need to be invoked (for example, if it only gets executed when a user clicks on a button or the application reaches a certain state), I'd go with choice b. As I understand it, the function does consume memory and won't get garbage collected (which is a minus), but it also won't consume CPU resources until it's actually needed (which can be a huge plus).

  3. I'm not sure I understand the question, but I'd say it's not the same. If the only member of a is myName then the two are equivalent (both are occupying the global namespace. But if you have multiple properties, the savings become obvious. From your examples, I think it's clear you understand this, so again I may not understand the question.

Andrew
Actually, I am ready to use my memory and cpu when I need these variables. I just want to lazy load my string variables.
Sessizlik
I'm a huge fan of lazy loading, but as far as I know, lazy loading only helps if you want to put off large "resource allocation" (memory use, cpu use and/or execution time). Using a function that only returns a string literal (`"Rebecca"`) will use more memory, take longer to parse and take longer to return than just using a straight string literal. How much more? Well, probably very, very little. I just wouldn't call it a best practice.
Andrew
Maybe you noticed my prerequisite at the beginning. I want to use just one javascript file for my all site. Loading this variables at all other pages does not make sense for me. That is why I started think another alternative.What about storing these data at some variables? Caching is not a simple solution?
Sessizlik
I don't think there's any real difference between `a.myName` and `a.myName()` if the only purpose is to return a string (especially a string literal). You still have an object `a` and `a` still has a property `myName`. Now, if a.myName() returns different values depending on where you are in the site, then I'd say use it. Certainly a settings object might be what you're after, but honestly the only difference between `a.myName` and `a.myName()` is performance. I would highly recommend having a settings object for this sort of thing.
Andrew
But is not former allocating memory for String after parsing completed? The latter one is just allocating memory when I will call b.myName()? And can you give me example about settings object? Because as far as I know it is same as object.
Sessizlik
It still needs to allocate memory to hold the contents of the function until it is called (and returns). I don't know if a JS interpreter will allocate the same amount of memory to a string literal inside an uncalled method (there may be more overhead or there could be less; I don't know), but it still needs to store `"Rebecca"` somewhere. Plus it needs to store the method. I'd be shocked if it used less memory. But you have to admit it also will take longer to parse and longer to return than a straight variable or object property. And you're right. A settings object is just an object.
Andrew
I think you are right. Maybe in another question we should ask about how is javascript interpreter handling memory allocation of functions and global variables.Thanks
Sessizlik