-1

I'm trying to verify if a function already exists, so I don't create a new function with the same name:

// check if function exists before
alert (typeof window.testfunc) // before creating SHOULD alert "undefined"

function testfunc(){
  // do something
}

// check if function exists after
alert (typeof window.testfunc) // after creating the function ok to alert "function"

But it always alerts "function", even when the alert runs before defining the function.

I did try to console.log[window] before creating the function and it's already in the window object.

What am I missing? How to check if function already exists before creating a new function?

EDIT

Thanks everybody for teaching me about Hoisting! It will be helpful in future coding. This way seems to work (I understand than that Hoisting doesn't apply when there is an if statement)

alert (typeof window.testfunc) // NOW it alerts "undefined"
if (typeof window.testfunc !== "function"){
    function testooo(){
    }
}  
alert (typeof window.testfunc) // ok alerts "function"
codeispoetry
  • 373
  • 2
  • 13
  • 6
    https://developer.mozilla.org/en-US/docs/Glossary/Hoisting – David Aug 02 '23 at 18:19
  • 1
    The alert always shows "function" because function declarations are hoisted, meaning they are moved to the top during execution. – Jon Zavialov Aug 02 '23 at 18:21
  • 2
    Can you give an example of when you could "create a new function with the same name"? Your ide should alert you of duplicate function names, and you can always use `const func = () => {};`/`const func = function() {}` to guarantee there's not a duplicate named function. – Samathingamajig Aug 02 '23 at 18:24
  • Thanks! I had this kind of "intuition", but I did not know about Hoising. How to check if function already exists before creating a new function then? Should I declare a global variable and then check if variable is already set? – codeispoetry Aug 02 '23 at 18:26
  • Problem is in a way you define this function. Try `window.testfunc = function testfunc(){}`. This way you avoid hoisting – ponury-kostek Aug 02 '23 at 18:27
  • 2
    @codeispoetry: *"How to check if function already exists before creating a new function then?"* - Given the code shown, the function will be defined (because of hoisting) before any code **can** check. Which makes this a bit of a non-issue in the first place. This sounds very much like an XY Problem. Can you clarify the underlying problem you're trying to solve? – David Aug 02 '23 at 18:33

1 Answers1

1

I'm trying to verify if a function already exists, so I don't create a new function with the same name

You shouldn't. You can't know what the existing function does or where it comes from, so the best bet is actually to overwrite it. If you don't, your script will break for hard-to-debug reasons at some point.

Instead, if you don't want your function to get re-created unnecessarily, you should just make sure that your script gets loaded only once. Also, you should know your application code well enough to be certain that only one function with this name exists, or verify it statically through use of scopes and lexical analysis. You shouldn't need to verify at runtime.

This way seems to work

if (typeof window.testfunc !== "function"){
    function testooo(){
    }
}  

Actually no, it doesn't. It works only in sloppy mode, in really weird ways, and should not be used. You should always "use strict" mode instead. For example:

"use strict"; // begin your script with this
alert(typeof testfunc) // "undefined"

{ // a block scope
    function testfunc(){
        // do something
    }
 
    alert(typeof testfunc) // "function"
    … // code that uses `testfunc` goes here (and only here)
}

alert (typeof testfunc) // "undefined" still
Bergi
  • 630,263
  • 148
  • 957
  • 1,375