0

Question:

I was trying to figure out way in javascript that a person would create a variable and then set it equal to a function. Cannot you just create a function called main().

  function main() {
     //....code here
  }

code:

var main = function () {
        var now = Date.now();
        var delta = now - then;

        update(delta / 1000);
        render();

        then = now;

        // Request to do this again ASAP
        requestAnimationFrame(main);
    };*/

Code Here that I am not sure why it is enclosed in (...code here...)

(function() {
    "use strict";
      // Doesn't work because strict mode enforces 
      // the fact that the second example shouldn't work
    if (true) {
        function fn() {
            // This won't run
            console.log("doesn't work");
        };
        fn();
    }
})();
Doug Hauf
  • 2,775
  • 8
  • 38
  • 65
  • Yes, you could do using typical declaration syntax. Sometimes the reason for assigning a function to a variable is that declarations are not technically allowed inside blocks since they're not a statement. Other times one may not be certain that they actually need the function, so they defer its creation until later if needed. – cookie monster Jul 01 '14 at 18:43
  • The accepted answer to the duplicate question is inaccurate and doesn't actually answer the question of when/why one would use one syntax over the other. – cookie monster Jul 01 '14 at 18:48
  • Can you show and example that would work and an example of one that will not work? – Doug Hauf Jul 01 '14 at 19:00
  • If the function is a variable then it does not create the function until that it is called at least once. – Doug Hauf Jul 01 '14 at 19:01
  • [Here are examples](http://jsfiddle.net/gS8CZ/) of code that works, shouldn't work, and doesn't work (commented out). If the function is a variable, then it doesn't create the function until the assignment operation takes place. `var foo; /*no function*/` ... `foo = function(){}; /*now there's a function*/` – cookie monster Jul 01 '14 at 19:13
  • What happened to the fiddle link? Why on the last function is it enclosed in a (function Name() ... this is one aspect of Javascript that I am not completely certain of. – Doug Hauf Jul 01 '14 at 19:51
  • The link is still there. Doesn't it work for you? The last one is enclosed in an IIFE *(Immediately Invoked Function Expression)* just so that I could create a new variable scope that defines *strict mode*. The `"use strict"` declarative activates strict mode within the scope where it's defined. [Here it is](http://jsfiddle.net/gS8CZ/1/) alone without the outer function. I only had it the first time so that the second example was certain to run. – cookie monster Jul 01 '14 at 22:04
  • @cookiemonster Per the Stack Overflow guidelines, feel free to edit the duplicate question and/or answer in the duplicate to make them better. – Phrogz Jul 02 '14 at 01:51
  • @Phrogz: I'm not very good at following guidelines. I like to make up my own as I go. – cookie monster Jul 02 '14 at 02:11

0 Answers0