0

I would like to know what is hide under some specifics ways to execute some code.

I have 3 different ways to do in my mind, but i really don't know which is better on what kind of situation and why. I just choose the Case 2 method most of the time for a syntaxical preference, but i am more and more wondering what are the advantages and disadvantages of each method.

Case 1:

var response = Func();
//Handle response...

Case 2:

Func(response){
  //Handle response...
};

Case 3:

Func({
  case_x: function(opt){
    //Handle an example case...
  }
});

Thanks for the explanations i am very curious about that !

Ludo
  • 5,060
  • 15
  • 53
  • 85

1 Answers1

0

Assuming that Func is an existing function:

Case 1 is if your function returns a value and you want to store the result, such as in:

function times_ten(num){ return num*10; }
var ten = 10;
var hundred = times_ten(ten);

So now you can use the variable hundred from now on.

Case 2 is if you are not returning a value, or you don't need to save it:

function write_times_ten(num){ num = num*10; document.write(num);  }
var ten = 10;
var hundred = write_times_ten(ten);

Case 3 is slightly more complex.

If you do var something={a:function(){}} - By creating an object like this, you have all of your functions in the same scope, so you don't pollute the global namespace. This is kind of like creating a class in other languages. jQuery is the biggest example of this I can think of, because for all of what it does, it all is processed by the object jQuery (or calling a function such as jQuery.parseJSON(json_string)

If you're passing in an object with a function like that, then you are asking Func() to do something with that function you passed in, like so:

function do_something_times_ten(options){
    options.num = options.num*10;
    options.do_something(num);
}
var ten = 10;
do_something_times_ten({num:ten, 
                        do_something:function(num){
                                                     num = num*10;
                                                     document.write(num);  
                                                  }
                        }
);

Which would now write out 1000, not 100.

Max
  • 2,082
  • 19
  • 25
  • Thanks for your answer, but my question is about the performences of these methods, i already know how to use it. I am wondering if one is more quick or not, if they are all asynchron, this kind of things. Does the fact of not pulluting the global namespace has some repercutions ? – Ludo Jan 11 '13 at 01:31
  • If we don't pollute the global namespace, everything we create in the scope will be destroyed after the execution so our app is "lighter" by doing like this right ? – Ludo Jan 11 '13 at 01:40
  • 1
    No, if you have it scoped like that, you can only access methods through the object- i.e you can do jQuery.parseJSON(json_text) but not parseJSON(json_text) - Here is an awesome explanation of why pollution is bad, especially in bigger projects: http://stackoverflow.com/questions/8862665/what-does-it-mean-global-namespace-would-be-polluted – Max Jan 11 '13 at 03:17
  • In terms of performance: case1 is going to use more memory than case2 by virtue of both calling Function, but a new variable outside of the function which wont be garbage collected. Case 3 will use up more memory than case2, assuming they both only have the one method, but case3 is really a special more advanced case – Max Jan 11 '13 at 03:23