JS – Object – Class – Method

Enter your car details below:

Source Code

<input id=”year” placeholder=”Year”></input>
<input id=”brand” placeholder=”Brand”></input>
<input id=”model” placeholder=”Model”></input>
<button id=”btn”>go</button>


<p id=”demo”></p>

<script>

var year= document.getElementById(“year”);
var brand= document.getElementById(“brand”);
var model= document.getElementById(“model”);
var btn= document.getElementById(“btn”);

btn.addEventListener(“click”,function(){

class Car {
constructor(year,brand,model) {
this.year = year;
this.brand = brand;
this.model = model;

}


present(x) {
return x + “, I have a ” + this.year+ ” ” + this.brand+ ” ” + this.model;
}
}

mycar = new Car(year.value, brand.value,model.value);


document.getElementById(“demo”).innerHTML = mycar.present(“Hello”);
})
</script>

Comments are closed.