これを理解しようとしているときに出会った このリンクから取得しました。
関数は次のとおりです(自分で理解できるように少し変更しました):
(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 がこれら 2 つの機能をどのように一緒に追加するのか理解できません。
誰かがこれがどのように機能するかを少し理解するのを手伝ってくれますか、または少なくとも、もう少し理解しやすい方法で再構築するのを手伝ってくれませんか?
出力は次のとおりです。
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}
ありがとう、再帰はおそらく大きな初歩的な質問であることを知っており、何度か使用しましたが、それがどのように機能するかを理解すると頭が回転します。