Isn't the createTreeText() function supposed to take the text variable from function where it's called
No, not at all. Functions close over the variables in scope where they're created, not where they're called. All functions get from where they're called is the information passed to them as arguments (and sometimes this, depending on how they're called and what kind of function they are).
This example may clarify, see comments:
function wrapper() { var a = Math.random(); function foo() { // `foo` closes over `a`, because `a` is in scope // where `foo` was created console.log("a = " + a); // `foo` does not close over `b`, because `b` is not in scope // where `foo` was created try { console.log("b = " + b); // throws ReferenceError } catch (e) { console.log("error: " + String(e)); } } function bar() { var b = Math.random(); // Calling `foo` here does nothing to grant it access to `b` foo(); } bar(); } wrapper();
textas a parameter?textis an unknow variable, because it is defined in the first function only.