-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-prototype.js
41 lines (32 loc) · 974 Bytes
/
5-prototype.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#5 Protoype
function Mobil(merk, kecepatan){
this.merk = merk;
this.kecepatan = kecepatan;
}
Mobil.prototype.jarak = function(jam){
return `Mobil ${this.merk} berjalan ${jam} jam menempuh jarak ${this.kecepatan * jam} km.`;
}
let honda = new Mobil("Honda", 120);
console.log(honda.jarak(2));
Output:
"Mobil Honda berjalan 2 jam menempuh jarak 240 km."
<html>
<button onclick="count()">Count</button>
<input type="number" id="time">
<p id="output">0</p>
</html>
<script>
function Mobil(type, kecepatan){
this.type = type;
this.kecepatan = kecepatan;
}
Mobil.prototype.jarak = function(jam){
return `Setelah bergerak selama ${jam} jam, mobil ${this.type} berada pada jarak sejauh ${jam * this.kecepatan} km.`
}
let honda = new Mobil("Honda", 120);
function count(){
var jam = document.getElementById("time").value;
console.log(jam);
document.getElementById("output").textContent = honda.jarak(jam);
}
</script>