-1

Not sure if this is the best way to name this question but I have the following code which does not like to run in strict mode. I need to set the contents/statements of the runstatementshere() function, depending on the if condition. Then runstatementshere() will run as part of another function and be part of that scope.

    if (i == true) {

        function runstatementshere() {

            // multiline commands
        };

    }

    else {

        function runstatementshere() {

        }

    }

    function somecode() {

        runstatementshere();

    }
Zertix.net
  • 55
  • 7
  • 2
    Why not put the `if` statement inside the `runstatementshere` function? – 4castle Oct 22 '16 at 21:02
  • 1
    Use a function expression to assign a function to a variable declared in a higher scope rather than a function declaration. Then, you can assign a different function implementation to the variable depending upon your `if` statement. – jfriend00 Oct 22 '16 at 21:13
  • You should never define a function inside a conditional – Cjmarkham Oct 22 '16 at 21:18
  • Because `somecode()` needs to run many times so I thought calling a function in there via `runstatementshere();` is faster than having an `if` evaluation all the time, be it a simple one. And I set my `runstatementshere();` content beforehand just once. – Zertix.net Oct 22 '16 at 21:20
  • There's no real need to consider one over the other in this case due to speed. If you start micro managing your code like this, you will never finish it. The speed benefits (if any) are negligible at best – Cjmarkham Oct 22 '16 at 21:24
  • i see.......... – Zertix.net Oct 22 '16 at 21:37

3 Answers3

0

Read this answer first.

Function declarations seem to be allowed in but hoisted within the containing block. I'm not sure if this is defined behaviour, but Chrome and Firefox act that way.

They behave that way because if the two functions were hoisted within the scope of the if statement, the latter would always win.

Anyway, to solve your issue, use function expressions.

"use strict";

var runstatementshere;
if (true) {
  runstatementshere = function() { console.log("a"); }
} else {
  runstatementshere = function() { console.log("b"); }
}

function somecode() {
  runstatementshere();
}

somecode();
Community
  • 1
  • 1
pishpish
  • 2,574
  • 15
  • 22
-1

The easiest approach would be:

function firstFunction() {
  console.log(1);
}

function secondFunction() {
  console.log(2);
}

function somecode() {
  if (i) {
    firstFunction();
  } else {
    secondFunction();
  }
}

var i = false;

somecode();

But if you have a good reason not to use if inside somecode(), try this way of declaring functions.

The problem in your code is that you declare the same function twice in the same scope.

var runstatementshere, i = false;

if (i == true) {
  runstatementshere = function() {
    console.log(1);
  };
} else {
  runstatementshere = function() {
    console.log(2);
  };
}

function somecode() {
  runstatementshere();
}

somecode();
Chris Dąbrowski
  • 1,902
  • 1
  • 12
  • 12
  • You should never define a function inside a conditional – Cjmarkham Oct 22 '16 at 21:18
  • @CarlMarkham: And why is that? – Felix Kling Oct 22 '16 at 21:18
  • Because they can cause inconsistencies and are not a part of ECMAScript .T – Cjmarkham Oct 22 '16 at 21:21
  • @CarlMarkham You should never use function declaration inside a conditional but why in you opinion one shouldn't use function expression? Expressions aren't hoisted. – Chris Dąbrowski Oct 22 '16 at 21:36
  • There is no need. The conditional should be inside of the function. I can't think of any instance where you would need to make an entire function conditional. My opinion of course. Would gladly be proven wrong. https://gist.github.com/cjmarkham/790b62f782746bb9cdc17edfec6c0e8a – Cjmarkham Oct 22 '16 at 21:41
  • @CarlMarkham Maybe you're right although I'm usually careful with _I can't think of any instance_ :) Hope you appreciate my edit. – Chris Dąbrowski Oct 22 '16 at 21:55
-2

This should compile in strict mode.. but is somehow nonsensical.. as it would be more logical to include a condition in the function body. But still you may have a good use for it.

(function(){

var myFunction = i ? function() {} : function () {};
function a()
{
  myFunction();
}}

)()

Rewirite it so you have a function constructor..

var myFunctionConstructor = function(i) {

     return i ? function() {} : function () {};
}


var myFunction = myFunctionConstructor(false);
myFunction(); // first function called
myFunction = myFunctionConstructor(true);
myFunction(); // second function called
Erik Simonic
  • 457
  • 3
  • 13