Created
April 11, 2025 14:37
-
-
Save hoadh/455d43f2c3d6474d94915adcfe1d8c37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
</head> | |
<body> | |
<button onclick="convertTemperature()">show</button> | |
<p id="output"></p> | |
</body> | |
<script> | |
// getter ==> Những phương thức lấy giá trị của thuộc tính | |
// - getCelsius | |
// setter ==> Những phương thức cập nhật giá trị của thuộc tính | |
// - setCelsius | |
class Temperature { | |
constructor(celsius) { | |
this.celsius = celsius; | |
} | |
setCelsius(celsius) { | |
if (celsius < -273) { | |
alert("Giá trị đầu vào không hợp lệ"); | |
} | |
this.celsius = celsius; | |
} | |
getCelsius() { // Đây là 1 setter | |
return this.celsius; | |
} | |
toF() { | |
return 100; | |
} | |
toK() { | |
return 200; | |
} | |
} | |
function convertTemperature() { | |
let temp = new Temperature(25); | |
// temp.celsius = -280; // Sai logic . Vẫn cập nhật được giá trị. | |
temp.setCelsius(-280); // để cập nhật giá trị cho thuộc tính celsius | |
document.getElementById("output").innerHTML = "Độ F là: " + temp.toF() + "<br>" + "Độ K là: " + temp.toK(); | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment