Skip to content

Instantly share code, notes, and snippets.

@aepyornis
Last active October 6, 2015 15:28
Show Gist options
  • Save aepyornis/1d086dc4c0282a8c35e1 to your computer and use it in GitHub Desktop.
Save aepyornis/1d086dc4c0282a8c35e1 to your computer and use it in GitHub Desktop.
function doSomething() {
var someValue = "this is an important value";
anotherFuctionDefinedElsewhere();
}
function anotherFuctionDefinedElsewhere () {
// this function can't access someValue
console.log(someValue);
}
/// typically this can be easily solved by passing someValue into the function:
function doSomething() {
var someValue = "this is an important value";
anotherFuctionDefinedElsewhere(someValue);
}
function anotherFuctionDefinedElsewhere (someValue) {
// now this function can access someValue
console.log(someValue);
}
// Is there a way to have anotherFuctionDefinedElsewhere() access a local variable of doSomething() without passing?
@aguestuser
Copy link

if i understand correctly something like this might work:

function makeChangeableFunc(importantValue){
  return _.partial(changeableFunc(importantValue));
}

function changeableFunc(value, otherValue){
  return value + otherValue;
}

var fn = makeChangeableFunc("this is really important");

fn(", but this is just whatever."); // => "this is really important, but this is just whatever."
fn(", and so is this!"); // => "this is really important, and so is this!"

@aepyornis
Copy link
Author

another way to pass values between functions defined separately

function doSomething() {
  this.someValue = "this is an important value";

  anotherFuctionDefinedElsewhere.apply(this);

}
function anotherFuctionDefinedElsewhere () {
    console.log(this.someValue);
}

doSomething();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment