JS |方法 |在第三个函数中添加两个函数的结果 |解决方案?
JS | Methods | Add the result of two functions in a third function | solution?
有人知道如何在一个方法中组合两个函数吗?我想在第三个函数中添加两个不同函数的结果并将其记录到控制台。感谢您的帮助。
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());
您得到函数的 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());
有人知道如何在一个方法中组合两个函数吗?我想在第三个函数中添加两个不同函数的结果并将其记录到控制台。感谢您的帮助。
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());
您得到函数的 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());