I am trying to print out the gass method from the class Bil. I want it to print out when clicking a button in HTML. I get an error saying
Uncaught ReferenceError: gass is not defined at HTMLButtonElement.onclick
(index.html:9).
What could be the reason for this error?
// My HTML Code
<button onclick="gass()">Gass for Volvo</button>
<p id="test"></p>
<script src = "script.js"></script>
//javascript code
class Bil{
constructor(registeringsnr, merke, yearmodel, hastighet)
{
this.registeringsnr = registeringsnr;
this.m = merke;
this.yearmodel = yearmodel;
this.hastighet = hastighet;
}
gass()
{
var nyHastighetettergass = this.hastighet +10;
return nyHastighetettergass;
}
brems()
{
var nyHastighetetterbrems = this.hastighet - 10;
return nyHastighetetterbrems;
}
}
var volvo = new Bil(100,"volvo",2018,1);
var ferrari = new Bil(200, "ferrari", 2011, 0);
var lada = new Bil(300,"lada", 2012,0);
document.getElementById("test").innerHTML = gass();
Onclick=“new Bil(.....).gass()”you’re trying to execute the functiongassthat was defined within classBil, therefore,gassfunction is out of scope from onclick of that button.document.getElementById("test").innerHTML = volvo.gass();.