取自此链接,我在尝试解决此问题时遇到了该链接。
这是功能(稍作修改以尝试帮助自己理解):
(function(){
fibonacci = (function () {
var cache = {};
return function (n) {
var cached = cache[n];
if (cached) {
console.log('already in the ', cache);
return cached;
}
if (n <= 1) {
console.log('no 0s or 1s, ', n);
return n;
}
console.log('a brand new ', n, 'consider yourself cached');
cache[n] = fibonacci(n - 2) + fibonacci(n - 1);
console.log('current cache: ', cache);
return cache[n];
};
}());
fibonacci(20);
})();
我对其进行了一些修改以尝试帮助自己理解,但我迷路了,因为输出趋于 0,然后它从 0 增加。我会认为在这个声明中:
cache[n] = fibonacci(n - 2) + fibonacci(n - 1);
fibonacci(n - 2)
将被评估,然后fibonacci(n - 1)
在此之后。
但即使是这样,我也不明白 JavaScript 如何将这两个函数加在一起。
任何人都可以帮助我理解它是如何工作的,或者至少,你可以帮助我以一种更容易理解的方式重组它吗?
这是输出:
a brand new 20 consider yourself cached
a brand new 18 consider yourself cached
a brand new 16 consider yourself cached
a brand new 14 consider yourself cached
a brand new 12 consider yourself cached
a brand new 10 consider yourself cached
a brand new 8 consider yourself cached
a brand new 6 consider yourself cached
a brand new 4 consider yourself cached
a brand new 2 consider yourself cached
no 0s or 1s, 0
no 0s or 1s, 1
current cache: Object {2: 1}
a brand new 3 consider yourself cached
no 0s or 1s, 1
already in the Object {2: 1}
current cache: Object {2: 1, 3: 2}
current cache: Object {2: 1, 3: 2, 4: 3}
a brand new 5 consider yourself cached
already in the Object {2: 1, 3: 2, 4: 3}
already in the Object {2: 1, 3: 2, 4: 3}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8}
a brand new 7 consider yourself cached
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8}
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21}
a brand new 9 consider yourself cached
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21}
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55}
a brand new 11 consider yourself cached
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55}
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144}
a brand new 13 consider yourself cached
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144}
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377}
a brand new 15 consider yourself cached
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377}
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987}
a brand new 17 consider yourself cached
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987}
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584}
a brand new 19 consider yourself cached
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584}
already in the Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584, 19: 4181}
current cache: Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584, 19: 4181, 20: 6765}
谢谢,我知道递归可能是一个很大的菜鸟问题,我已经使用过几次了,但是了解它的工作原理让我头晕目眩。