if function does not exist write function - javascript

前端 未结 2 740
生来不讨喜
生来不讨喜 2020-12-30 05:34

In php I\'m writing the following


In javascript I wish to test if the

2条回答
  •  星月不相逢
    2020-12-30 05:51

    You should use strict comparison operator !==

    if(typeof myFunction !== 'function'){
        window.myFunction = function(){}; // for a global function or
        NAMESPACE.myFunction = function(){}; // for a function in NAMESPACE 
    }
    

    Also try to keep js functions inside namespaces, this way you avoid collisions with other js libraries in the future.

提交回复
热议问题