var student001 = {
name: 'sherlock',
scoreA: 90,
scoreB: 80,
scoreSum: scoreSum
};
var student002 = {
name: 'lily',
scoreA: 80,
scoreB: 70,
scoreSum: scoreSum
};
function scoreSum () {
return `name: ${this.name}, total: ${this.scoreA + this.scoreB}`;
}
console.log(student001.scoreSum());
console.log(student002.scoreSum());
생활코딩 this 강의를 보고 예제를 만들어 보았다.
https://www.opentutorials.org/module/4047/24607
protype
// constructor
function Students (name, scoreA, scoreB) {
this.name = name;
this.scoreA = scoreA;
this.scoreB = scoreB;
}
Students.prototype.sum = function () {
return this.scoreA + this.scoreB;
};
var stu001 = new Students('sherlock', 100, 90);
var stu002 = new Students('lily', 80, 60);
console.log(`stu001.sum(): ${stu001.sum()}`); // stu001.sum(): 190
console.log(`stu002.sum(): ${stu002.sum()}`); // stu002.sum(): 140
stu001.sum = function () {
return `plus 10: ${this.scoreA + this.scoreB + 10}`;
};
console.log(`modified stu001.sum(): ${stu001.sum()}`);
// modified stu001.sum(): plus 10: 200
https://www.opentutorials.org/module/4047/24610#_=_
'programming > javascript' 카테고리의 다른 글
closure, callback 예시 만들기 (0) | 2019.08.29 |
---|---|
생활코딩 자바스크립트 class 강의를 보고 만든 예제 (0) | 2019.08.24 |
Mac os brew 이용 nodejs 설치하기 (0) | 2019.08.23 |
node js mysql 연동 에러 (0) | 2019.03.05 |
JS) call 예제 (0) | 2019.02.04 |