0

I have something like this:

function parent(one,two,three,yahoo) {
  blabla
  blabla
  function(what,where,how,one,two) {
     blabla
     //usage of what, where, how, one, two
     blabla
  }
  blabla
  blabla
}

So, as you can see, I want to pass the "one" and "two" from "parent" to "child".

How can I achieve this in cross-browser way, please?

thisismine
  • 3
  • 1
  • 2
  • 3
    You don't need to pass them. All the parent function's variables are in the scope of the child function. – Barmar Jul 24 '17 at 05:59
  • Please structure you question properly. And yes, all parent scope variables are available in the embedded child scope. – jagzviruz Jul 24 '17 at 06:01
  • parent function parameters are available to child function. Just don't create variables, parameters that have the same name as parent's parameters you want to use (they will shadow them). – marzelin Jul 24 '17 at 06:01

4 Answers4

2

The parent function's scope includes child functions, so the child can access them without them being passed. Just leave them out of the parameter list.

function parent(one,two,three,yahoo) {
  blabla
  blabla
  function(what,where,how) {
     blabla
     //usage of what, where, how, one, two
     blabla
  }
  blabla
  blabla
}

For more info, see What is the scope of variables in JavaScript?

Barmar
  • 741,623
  • 53
  • 500
  • 612
0
function parent(one,two,three,yahoo) {
 function a (what,where,how,one,two) {
console.log(one + two);
 }
  a(1,2,3,this.one,this.two);

}

parent(1,2,3,4);

Use 'this' to pass the parameters of the parent function.

Pranav Anand
  • 467
  • 6
  • 12
0

Inside your child function, you can call your parent variables wiothout passing them as a parameter in your child function...

Check this fiddle

function parent(one,two,three,yahoo) {
 console.log(one);
 console.log(two);
 console.log(three);
 console.log(yahoo);

  function child(what,where,how) {
 console.log(one);
 console.log(two); 
 console.log(what);
 console.log(where); 
 console.log(how);
  }

  child("what","where","how");
}

parent("1","2","3","yahoo");
Arunkumar G
  • 126
  • 6
0

You don't need to pass them. Parent functions variable are accessible by the child function by default

var parent=function(one) {
  //something
  var child=function(one){
    alert(one);
  }
  child(one);
}

parent("this is the parent variable to child scope.");