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
this - JavaScript 객체 지향 프로그래밍
수업소개 메소드 내에서 메소드가 속한 객체를 참조 할 때 사용하는 키워드인 this에 대해서 알아봅니다. 강의 코드 this.js (변경사항) var kim = { name:'kim', first:10, second:20, sum:function(){ return this.first+this.second; } } //console.log("kim.sum(kim.first, kim.second)", kim.sum(kim.first, kim.second))
www.opentutorials.org
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#_=_
prototype - JavaScript 객체 지향 프로그래밍
수업소개 JavaScript의 prototype이 필요한 이유와 prototype을 통해서 코드의 재사용성과 성능을 향상시키는 방법을 알려드립니다. 강의1 prototype이 필요한 이유를 소개합니다. 강의2 prototype을 이용해서 코드의 재사용성을 높이고, 성능을 향상시키는 방법을 소개합니다. 코드 prototype.js (변경사항) function Person(name, first, second, third){ this.name=name;
www.opentutorials.org
'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 |