开发者

Javascript: Change value of variable inside conditional inside function

I'm trying to reuse a complicated function, and it would work perfect开发者_JAVA技巧ly if I could change the value of a local variable that's inside a conditional inside that function.

To boil it down:

var func = complicated_function() {
    // lots of code
    if (something) {
        var localvar = 35;
    }
    // lots of code
}

I need localvar to be some other number.

Is there any way to assign localvar to something else, without actually modify anything in the function itself?

Update: The answer is yes! See my response below.


Is there any way to assign localvar to something else, without actually modify anything in the function itself?

Nope.


No, but it is possible to assign it conditionally so that the function signature (basically, the required input and output) does not change. Add a parameter and have it default to its current value:

var func = complicated_function(myLocalVar) {
    // lots of code
    if (something) {
        // if myLocalVar has not been set, use 35.
        // if it has been set, use that value
        var localvar = (myLocalVar === undefined)?35:myLocalVar;
    }
    // lots of code
}


No.

Without changing the complicated function there is no way, in javascript you can manipilate this by using call and apply. You can override functions in the complicated function or add new if this is an option (but they won't be able to access the local variable localvar).

this is more for fun my real answer is still no.

If you are feeling crazy :)

var complicatedFunction = function() {
  var i = 10;

  var internalStuff = function() {
    console.log(i); // 10 or 12?
  };

  return internalStuff();

};

var complicatedFunction;

eval("complicatedFunction = " + complicatedFunction.toString().replace(/i = 10/, 'i = 12'));

complicatedFunction(); //# => 12


If the function uses this.localvar:

var func = function() {
    alert(this.localvar)
    if (true) {
        var localvar = 35;
    }
    // lots of code
    alert(this.localvar)
}
var obj = {localvar: 10}; 
func.call(obj); // alerts 10 twice

If not, then you can't change it without changing the function.


In javascript variables are "pushed" to the top of their function. Variables in javascript have function scope, not "curly brace" scope like C, C++, Java, and C#.

This is the same code with you (the developer) manually pushing it to the top:

var func = complicated_function() {
   var localvar = 0;
   // lots of code
   if (something) {
       localvar = 35;
   }
   // lots of code
}

Does declaring the variable "up" one function help you out? At least the declaration is isolated.

function whatever() {
  var localvar = 0;

  var func = function() {
    var something = true;

    // lots of code
    if (something) {
        localvar = 35;
    }
    // lots of code
  };

 func();
 alert(localvar);
}
whatever();

Here is the jsFiddle: http://jsfiddle.net/Gjjqx/

See Crockford:

http://javascript.crockford.com/code.html

JavaScript does not have block scope, so defining variables in blocks can confuse programmers who are experienced with other C family languages. Define all variables at the top of the function.


I asked this question about three weeks ago and within a half hour got five answers that all basically told me it wasn't possible.

But I'm pleased to announce that the answer is YES, it can be done!

Here's how:

var newfunc = func.toString().replace('35', '42');
eval('newfunc = ' + newfunc);
newfunc();

Of course, it uses eval, which probably means that it's evil, or at least very inadvisable, but in this particular case, it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜