I’ve just started out trying to do some JS and have hit a snag I don’t understand.
I have a JS file that will hold calculations that I can reuse. In this file it has 2 functions - testone and testtwo. Testtwo uses testone, calling it twice. But according to the output error the second call to it results in the error “Uncaught TypeError: testone is not a function”. This is despite the fact that testone clearly works the first time around. What am I missing here? TIA
Haha fair enough. Don’t have access to the code right now but this is the gist of it. It’s not designed to do anything useful, its just to give myself a framework to work with.
function testone(testing){
if (testing="y") {
return testone=1000;
}else{
return testone=2000;
}
}
function testtwo (testing){
var passone = testone("y");
var passtwo = testone("n");
if (passtwo < passone) {
return testtwo = passtwo;
}else{
return testtwo = passone;
}
}
The first time you call testone, it returns 1000, but it also sets testone to be 1000.
The second time you try to call testone, it now equals 1000 – it is no longer a function. That’s why you get an error.
You should never redefine a function inside of itself. Instead you can just return a number.
if (testing==="y") {
return 1000;
}else{
return 2000;
}
Actually, that redefinition, when done intentionally, is wildly powerful. I mean, you did this accidentally, but later on, when you hear folks talk about memoization, you can say “Psssh, been there done that. Waaaaay back when.”
It’s an advanced topic, i don’t believe freecodecamp gets into it, but that’s what you’re doing: replacing the original function with something else.
This may not exactly suit your needs, bu if you ever need a value that requires a function, but will have no need of the function afterwards you can use a self calling anonymous function such as:
let n = 10;
let val = (() => n * 10)()
console.log(val) //100
if you are unfamiliar with that syntax this works the same:
let n = 10;
let val = (function () {
return n * 10;
})()
An example of a memoized function might be useful. Back in the days of the browser wars, maybe fifteen years back, AJAX (talking to the server to get bits of data, and dynamically updating the page) was a very new and very cool concept. And every browser had a different mechanism to do it. The function name was different, the parameters and their order was different, how the thing was handled was different… It was a nightmare. And if you were cutting edge, you might use this thing a lot. Like dozens of times! (We were so naive then…)
So rather than have to ask the javascript engine “what was your data-from-the-server function called again?” dozens of times, it might be handy to have a function like this:
var fetch = function(url, data){
if (/* check if this is IE... */){
// if it is, we'll replace the fetch with that exact function
fetch = /* IE AJAX function */
} else if(/* is this Netscape? */){
// replace the fetch function with Netscape's implementation
fetch = /* Netscape's version */
} else {
// finally, the everything else version
fetch = /* fallback for everything else */
}
// at this point, we won't use this if again
// on this page - fetch() is now browser-
// specific!
// if we call it again, it will use the "memorized" function.
return fetch(url, data);
}
It’s simplified, but the point is, we only check all those branches the first time! That first time, we assign the proper function dynamically, removing the original entirely.