【问题标题】:JS | Methods | Add the result of two functions in a third function | solution?JS |方法 |在第三个函数中添加两个函数的结果 |解决方案?
【发布时间】:2018-03-23 22:30:41
【问题描述】:

有人知道如何在一个方法中组合两个函数吗?我想在第三个函数中添加两个不同函数的结果并将其记录到控制台。感谢您的帮助。

function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        return Math.PI * this.radius * this.radius;
        
    };
    // define a perimeter method here
    this.perimeter = function () {
        return 2 * Math.PI * this.radius;
    }
    this.logg = function() {
        return this.perimeter + this.area;
    }
};

var perimeter = new Circle(12);
perimeter.perimeter();
//doesn't work
console.log(perimeter.logg());

【问题讨论】:

    标签: javascript function object methods addition


    【解决方案1】:

    您将得到函数的toString 结果的串联。你忘了调用函数 - return this.perimeter() + this.area()

    function Circle (radius) {
        this.radius = radius;
        this.area = function () {
            return Math.PI * this.radius * this.radius;
            
        };
    
        this.perimeter = function () {
            return 2 * Math.PI * this.radius;
        };
        
        this.logg = function() {
            return this.perimeter() + this.area();
        };
    };
    
    var perimeter = new Circle(12);
    perimeter.perimeter();
    //doesn't work
    console.log(perimeter.logg());

    【讨论】:

    • 啊,现在是早上,这就是为什么 :) 感谢您的及时解决。
    • Suren Srapyan 当然 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 2015-09-20
    • 2018-11-13
    • 2016-09-12
    • 2021-03-07
    • 2023-01-19
    相关资源
    最近更新 更多