Add a return type of string when a function is accessed like a variable

后端 未结 2 795
甜味超标
甜味超标 2021-01-27 14:26

I am not sure if this is possible (in fact I\'m struggling to work out what to google for) but here it goes:

I have a function

const test = () => {
           


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-01-27 15:02

    You can do this if you first check the type of the variable, in particular you will check if its a Function:

    function isFunc(functionToCheck) {
     return functionToCheck && {}.toString.call(functionToCheck) === '[object Function]';
    };
    

    So if this is true, then your variable is infact a function and you can return the stuff you need from it:

    const isItAFunction = somevariable;
    
    if(isFunc(isItAFunction)) {
       // return your string
    } else {
      // do something else
    }
    

提交回复
热议问题