0

Is it possible to do the following in some other way, without passing parameters?

var f = function(){
    console.log(v);
}

var original = f;

f = function(){
    var v = "test";
    original();
};

// the following line is called by 3rd party code
f();  // it errors out here - v is not defined
1
  • without passing parameters, Global. Make v a global variable. Commented Nov 7, 2016 at 5:08

3 Answers 3

1

Make var v as a globel variable

var v ;
var f = function(){
  v="new test"
    console.log(v);
}

var original = f;

f = function(){
     v = "test";
    original();
};

f();//f function
original();//original function also working

Sign up to request clarification or add additional context in comments.

3 Comments

can JS engine, in theory, interrupt execution between v = "test"; and original(); and replace v with another value?
@MaximBalaganskiy Ya..var v is the globel variable .so easily replace value of that
@MaximBalaganskiy see the updated answer .The v replace with another value on original function call
0

Use can use JavaScript Closures here

function f() {
    var v = 'test';
    return (function () {
        console.log(v)
    })();
}

Comments

0

Another approach here:

var f = function(){
    console.log(arguments[0]);
};

f = f.bind(null, 'test');

f();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.