4

Note: This is mostly a theoretical practice.

function one() {
    return [1, function() { one(); }];
}
console.log((one()[1])());

The output gives undefined. Why?

4

1 回答 1

7

拆分它:

function one() {
    return [1, function() { one(); }];
}
console.log((one()[1])());

one(); // [1, function() { one(); }]
[1] // function() { one(); }
() // undefined

如果您返回one(),它将返回数组:

function one() {
    return [1, function() { return one(); }];
}
console.log((one()[1])());

one(); // [1, function() { return one(); }]
[1] // function() { return one(); }
() // [1, function() { return one(); }]
于 2013-06-01T02:33:47.243 回答