6.7 ํจ์๋ฅผ ์ฌ์ฉํ ๊ฐ์ฒด ์์ฑ
๊ฐ์ฒด๋ฅผ ํ๋์ฉ ์ง์ ๋ง๋ค์ด ๋ฐฐ์ด์ ๋ฃ์ผ๋ฉด ์๋ก ๋ค๋ฅธ ํํ์ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด ์์ ๋ฃ์ ์ ์๋ค๋ ์ฅ์ ์ด ์์ง๋ง, ๋ฒ๊ฑฐ๋ฝ๊ณ , ์ด๋ ค์ฐ๋ฉฐ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆฐ๋ค. ์ด๋ฐ ๊ณผ์ ์ ์ฝ๊ฒ ํ๊ธฐ ์ํ์ฌ ํ๋์ 'ํ'์ ๋ง๋๋ ํจ์๋ฅผ ๋ง๋ค์ด ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์ ๊ฐ์ฒด๋ฅผ ๋ง๋ ํ ๊ฐ์ฒด๋ฅผ ๋ฆฌํด์ํจ๋ค.
๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํจ์์ ๊ธฐ๋ณธํ์(1)
function makeStudent(name, korean, math, english, science) { var willReturn = { }; return willReturn; }
๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํจ์์ ๊ธฐ๋ณธํ์(2)
function makeStudent(name, korean, math, english, science) { var willReturn = { //์์ฑ ์ด๋ฆ: name, ๊ตญ์ด: korean, ์ํ: math, ์์ด: english, ๊ณผํ: science, //๋ฉ์๋ getSum: function () { return this.๊ตญ์ด + this.์ํ + this.์์ด + this.๊ณผํ; }, getAverage: function () { return this.getSum() / 4; }, toString: function () { return this.์ด๋ฆ + '\t' + this.getSum() + '\t' + this.getAverage(); } }; return willReturn; }
makeStudent()ํจ์๋ฅผ ์์ฑํด์ ๊ฐ์ฒด๋ฅผ ์ฐ์ด๋ด๋ฏ์ด ์์ฐํ ์ ์๋ค.
function makeStudent(name, korean, math, english, science) { /*์๋ต*/ } //ํ์์ ๋ณด ๋ฐฐ์ด ์์ฑ var students = []; students.push(makeStudent('ํ๊ธธ๋', 87, 99, 100, 50)); students.push(makeStudent('ํ๊ฐ๋', 85, 92, 80, 90)); students.push(makeStudent('ํ์๋', 86, 93, 90, 80)); students.push(makeStudent('ํ์ ๋', 83, 90, 10, 60)); //์ถ๋ ฅ var output = '์ด๋ฆ\t์ด์ \tํ๊ท \n'; for (var i in students) { output += students[i].toString() + '\n'; } console.log(output);













