How to call a Javascript function expression

Deadly 提交于 2019-12-13 04:42:39

问题


Can someone please help me understand how to call this JS function expression

var math = {
    'factorial': function factorial(n) {
        if (n <= 1)
            return 1;
        return n * factorial(n - 1);
    }
};

回答1:


Here you go. Should be self-evident:

var math = 
{
    'factorial': function factorial(n) 
    {
        if (n <= 1)
          return 1;
        return n * factorial(n - 1);
    }
};

var result = math.factorial(3);

// For the purposes of illustration, we will use document.write to output 
// the result. document.write is generally not a good idea, however!

document.write(result);


来源:https://stackoverflow.com/questions/28017025/how-to-call-a-javascript-function-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!