Hi, we have seen a bit of object-oriented programming in javascript and we have been asked to do an exercise to use inheritance. We have to calculate the volume of a cylinder in which we have to use the radius of a circle from a previous exercise. But I don't think I'm using it right because it doesn't show me anything.
js code calculate volume:
function calcularVolumenCilindro()
{
var radio = Circulo.prototype.radio = getRadio();
var altura = prompt("Introduce altura del cilindro: ")
var volumen = volumenCilindro(radio,altura);
document.write("El volumen del cilindro es: "+volumen);
}
VolumeCylinder js code:
function volumenCilindro(radio,altura)
{
return Math.PI*radio*radio*altura;
}
Am I using the correct way to call the getRadio() method of the class from another exercise?
Answering your question.
You are not using correctly. There is a part of the code that doesn't make sense:
If
getRadio
it's a method of the classCirculo
, then to call it you simply do, assuming you've created an object of that class:"Modern" Solution
You can use the new syntax of
Clase
.In this example I use
getters
andsetters
, but you can use class methods if that is more comfortable for you.NOTE The exponential operator (
**
) is part of the standard since version ES2016(ES7).