Javascript 函数不会返回数组

Javascript 函数不会返回数组,javascript,jquery,function,return,Javascript,Jquery,Function,Return,这里我有一个函数nArray。此函数必须返回bigArray JSON对象,但函数不返回bigArray 问题出在哪里?nArray是一个函数,因此需要通过添加括号来执行它: console.log(nArray()); $('pre').html(JSON.stringify(nArray(), null, 4)); 此时,您只是传递对函数的引用。您实际上并没有调用函数。这样做: console.log(nArray()); // ^^-----------no

这里我有一个函数nArray。此函数必须返回bigArray JSON对象,但函数不返回bigArray

问题出在哪里?

nArray是一个函数,因此需要通过添加括号来执行它:

console.log(nArray());
$('pre').html(JSON.stringify(nArray(), null, 4));

此时,您只是传递对函数的引用。

您实际上并没有调用函数。这样做:

console.log(nArray());
//                ^^-----------note the parentheses
$('pre').html(JSON.stringify(nArray(), null, 4));
//                                 ^^-----------and here

关于你的小提琴,我看不到任何元素,所以最后一行试图设置$'pre'的.html的代码不会起任何作用。

这个问题似乎离题了,因为它是由没有实际调用你的函数…face palm引起的
console.log(nArray());
//                ^^-----------note the parentheses
$('pre').html(JSON.stringify(nArray(), null, 4));
//                                 ^^-----------and here