如何调用Javascript函数表达式

时间:2015-01-19 02:10:52

标签: javascript function expression

有人可以帮我理解如何调用这个JS函数表达式

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

1 个答案:

答案 0 :(得分:1)

你走了。应该是不言而喻的:

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);