Forum Moderators: open
<div id="args"></div>
<script ...core.js"></script>
<script ...js_functions.js"></script>
function write_results(){
var d = document;
myp = d.createElement("p");
myp.appendChild(mytn);
myid.appendChild(myp); <<<ERROR GEN HERE
}
function jsfunctions_args(){
var d = document;
//function call with arguments
displayProjects("Bluebeam", "Artichoke", "Monarch", "Starfish", "PaperClip", "Naomi", "Butterfly");
myid = d.getElementById("args");
function displayProjects(){
for(var i = 0; i < displayProjects.arguments.length; i++){
//placed here the myid = ... works?
mytn = d.createTextNode(displayProjects.arguments[i]);
write_results();
}
}
}
myid = d.getElementById("args"); within the 'for code block' then the routine works but I was trying to avoid making multiple, what I considered unnecessary, references. write_results function ignoring global variables since it is nested within a function? I'm guessing, that's why I'm here, arrgh! Thanks all.
You can nest a function within a function. The nested (inner) function is private to its containing (outer) function. It also forms a closure.
A closure is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression). [1]
Since a nested function is a closure, this means that a nested function can "inherit" the arguments and variables of its containing function. In other words, the inner function contains the scope of the outer function.
To summarize:
The inner function can be accessed only from statements in the outer function.
The inner function forms a closure: the inner function can use the arguments and variables of the outer function, while the outer function cannot use the arguments and variables of the inner function.
function displayProjects(){
myid = d.getElementById("args");
for(var i = 0; i < displayProjects.arguments.length; i++){
mytn = d.createTextNode(displayProjects.arguments[i]);
write_results();
}
}
function jsfunctions_args(){
var d = document;
// set myid BEFORE calling displayProjects
myid = d.getElementById("args");
//function call with arguments
displayProjects("Bluebeam", "Artichoke", "Monarch", "Starfish", "PaperClip", "Naomi", "Butterfly");
function displayProjects(){
for(var i = 0; i < displayProjects.arguments.length; i++){
mytn = d.createTextNode(displayProjects.arguments[i]);
write_results();
}
}
}