function foo(a) {
if (/* Some condition */) {
// perform task 1
// perform task 3
}
else {
// perform task 2
// perform task 3
}
}
I have a function whose structure is similar to the above. I want to abstract task 3 into a function, bar()
, but I wish to limit the access of this function to only within the scope of foo(a)
.
To achieve what I want, is it right to change to the following?
function foo(a) {
function bar() {
// Perform task 3
}
if (/* Some condition */) {
// Perform task 1
bar();
}
else {
// Perform task 2
bar();
}
}
If the above is correct, does bar()
get redefined every time foo(a)
gets called? (I am worrying about waste of CPU resource here.)