var a,b,c;
function abc(a,b,c){
console.log("a is"+ a + ", b is"+ b + ", c is"+ c);
}
a; // undefined
b; // undefined
c; // undefined
I will be passing through strings a,b,c, but would like to also be able to access them once they are passed through the function.
So if I call abc("d","e","f"), I’d like that function to run using those three strings, but afterwards, outside of its own scope, I’d also like to be able to use a="d", b="e", and f="f".
Could someone explain how this is done? I think that even though they are declared outside of the function, the fact that it’s the same name is throwing me off.
I remember something in PHP about adding & before the parameter…
Thanks!
I’m not sure what you’re asking here, but it works just out of the box like you want it.
Since ECMAscript has function scope, the variables
a,bandcwill get locally resolved within the function context ofabc(). The outer declarations of thosevariable names (lets call them free variables), will never get accessed because the lookup process will find those names first within its *own context, resolve them and stop there.
If you just want to set those free variables which are part of a parent context, you probably should rename your formal parameters for convenience and then pass over the values
Hopefully that parent scope where
a,bandcare declared is not the global scope. That is very correctly still considered bad practice and karma, for a couple of reasons.