Discuss / JavaScript / 运行时候出错

运行时候出错

Topic source

Uncaught ReferenceError: n is not defined

var fib = {
    a: 0,
    b: 1,
    n: 0,
    max: 5,
    next: function () {
        var
            r = this.a,
            t = this.a + this.b;
        this.a = this.b;
        this.b = t;
        if (n < max) {
            n ++;
            return r;
        } else {
            return undefined;
        }
    }
};

将if(n<max) { b++; } 加上this后可以执行


  • 1

Reply