아래와 같이 두개의 텍스트 박스에서 숫자를 입력받아 사각형 면적을 구하는걸 간단하게 짰는데...
alert 창에는 숫자가 아니라 NaN이 나오네요 ㅠㅠ
(이놈의 자바스크립트는 디버깅도 안되니...)
입력값을 Number로 해서 숫자로 바꿨는데도 왜 저렇게 나오는지 도저히 알수가 없어서...
혹시나 조언 받을 수 있을까 해서 여기에 질문 드립니다.
============================================================================================
<!DOCTYPE html>
<html lang="en" xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script> //메인 함수 구성
//생성자함수 선언
function Rectangle(w, h) {
//변수 선언
var width = w;
var height = h;
//method 선언
this.getWidth = function () { return w; };
this.getHeight = function () { return h; };
this.setWidth = function (w) {
if (w < 0) {
throw '길이는 0일수 없습니다.';
} else {
width = w;
}
};
this.setHeight = function (h) {
if (h < 0) {
throw '길이는 0일수 없습니다.';
} else {
height = h;
}
};
};
Rectangle.prototype.getArea = function () {
return this.getWidth() * this.getHeight();
};
</script>
<script> //상속함수 구성
function Square(length) {
this.base = Rectangle;
this.base(length, length);
}
Square.prototype = Rectangle.prototype;
Square.prototype.constructor = Square;
</script>
<script> //입력 받은 값으로 계산
window.onload = function () {
var submit = document.getElementById("submit");
var row = Number(document.getElementById("row"));
var column = Number(document.getElementById("column"));
var rectangle = new Rectangle(row, column);
var square = new Square(row);
submit.onclick = function () {
alert("Rectangle is " + rectangle.getArea() + "\n" + "Square is " + square.getArea());
};
}
</script>
</head>
<body>
<p>Row <input type="text" id="row" /></p>
<p>Column <input type="text" id="column" /></p>
<button id="submit">Submit</button>
</body>
</html>