A Javascript Fibonacci (Generator) Function

Date: 2013-05-30

function fibonacci () {
    var f1 = 1;
    fibonacci = function () {
        var f2 = 1;
        fibonacci = function () {
            var f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
            return f3;
        }
        return f2;
    }
    return f1;
}

in ES6(Harmony), the generator keyword: yield will be introduced, a generator function will be like this:

function fibonacci(){
    var fn1 = 1;
    var fn2 = 1;
    while (1){
        var current = fn2;
        fn2 = fn1;
        fn1 = fn1 + current;
        yield current;
    }
}