首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >accumulator.value()末尾的“未定义”

accumulator.value()末尾的“未定义”
EN

Stack Overflow用户
提问于 2017-12-21 12:24:39
回答 2查看 100关注 0票数 3

我遇到了constructor function method this.result中的值错位问题。我不明白为什么我会得到function -undefined结束的结果.

请告诉我,在function中忘记了什么:

代码语言:javascript
复制
function Accumulator(startingValue) {
    this.startingValue = startingValue;

    this.read = function() {
        this.a = +prompt('Your digit: ', '');
    };

    this.value = function() {
        this.value += this.a;
    };

    this.result = function() {
        return this.value + this.startingValue;
    }
}

var accumulator = new Accumulator(1); // starting value 1
accumulator.read(); // sum prompt with current value
accumulator.read(); // sum current prompt with previous prompt and current value
console.log( accumulator.result() ); // display sum result
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-12-21 12:31:40

如果.value应该是一个整数,不要将它定义为一个函数:-)

我认为你应该放弃.value().startingValue.a,到处使用.value。将求和直接放入read方法中。简化为:

代码语言:javascript
复制
function Accumulator(startingValue) {
    this.value = startingValue;

    this.read = function() {
        // the temporary variable might be unnecessary but I left it in for verbosity
        const a = +prompt('Your digit: ', '');
        this.value += a;
    };

    this.result = function() {
        return this.value;
    };
}

var accumulator = new Accumulator(1); // starting value 1
accumulator.read(); // add prompt to current value
accumulator.read(); // add another prompt to current value
console.log( accumulator.result() ); // display sum by calling result() method

您还可能希望在原型上定义方法:

代码语言:javascript
复制
function Accumulator(startingValue) {
    this.value = startingValue;
}
Accumulator.prototype.read = function() {
    this.value += +prompt('Your digit: ', '');
};
Accumulator.prototype.result = function() {
    return this.value;
};

甚至使用现代的class语法,就像@ArtificialBug建议的那样:

代码语言:javascript
复制
class Accumulator {
    constructor(startingValue) {
        this.value = startingValue;
    }
    read() {
        this.value += parseInt(prompt('Your digit: ', ''), 10);
    }
    result() {
        return this.value;
    }
}
票数 5
EN

Stack Overflow用户

发布于 2017-12-21 12:32:57

有两个问题

代码语言:javascript
复制
this.value = function() {
    this.value += this.a; //this.value is a function
};

代码语言:javascript
复制
 console.log( accumulator.value ); // accumulator value is a function which needs to be invoked

搞定

代码语言:javascript
复制
function Accumulator(startingValue) {
    this.startingValue = startingValue;

    this.read = function() {
        this.a = (this.a || this.startingValue ) + +prompt('Your digit: ', '');//initialize and add the prompted value
    };

    this.value = function() {
        return this.a; //simply return the value
    };

    this.result = function() {
        return this.a + this.startingValue; //this.a instead of this.value
    }
}

var accumulator = new Accumulator(1); 
accumulator.read(); 
accumulator.read(); 
console.log( accumulator.value() ); // invoke the method

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47924965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档