7.4 ์บก์ํ
ํด๋ก์ ๋ฅผ ํ์ฉํ์ฌ width ์์ฑ๊ณผ height ์์ฑ์ ์บก์ํ
์์ฑ์ ํจ์ ์์์ width์ height๋ฅผ ์ ์ํ์ฌ ์ธ๋ถ์์๋ ์ง์ญ๋ณ์ width์ height๋ฅผ ์ฌ์ฉํ ์ ์๊ณ ์ค์ง ๊ฒํฐ(getter):getOO()ํํ์ ๋ฉ์๋์ ์ธํฐ(setter):setOO()ํํ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
throw๋ฅผ ์ฌ์ฉํ์ฌ ์ค๋ฅ๋ฅผ ๋ฐ์์์ผ 0์ดํ์ ๊ฐ์ด๋ฉด ๊ฐ์ ๋ก throwํค์๋๋ฅผ ์คํ์ํจ๋ค.
์บก์ํ๋ ๋ง์ผ์ ์ํฉ์ ๋๋ฐฐํ์ฌ ํน์ ์์ฑ์ด๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ์จ๊ฒจ๋๋ ๊ฒ์์ ์ฃผ์
function Rectangle(w, h) { var width = w; var height = h; } this.getWidth = function () { return w; }; this.getHeight = function () { return h; }; this.setWidth = function (w) { if( w < 0) { throw '๊ธธ์ด๋ ์์์ผ ์ ์์ต๋๋ค'; } else { width = w; } }; this.setHeight = function (h) { if( w < 0) { throw '๊ธธ์ด๋ ์์์ผ ์ ์์ต๋๋ค'; } else { height = h; }; } Rectangle.prototype.getArea = function () { return this.getWidth() * this.getHeight(); }; var rectangle = new Rectangle(5, 7); rectangle.setWidth(-2); console.log('AREA: ' + rectangle.getArea());











