1. 수업소개
3.2- 객체와 반복문
var memberArray = ["egoing", "graph", "lee"];
console.group("array loop");
var i = 0;
while (i < memberArray.length) {
console.log(memberArray[i]);
i++;
}
console.groupEnd();
var memberObject = {
manager: "egoing",
developer: "graph",
designer: "lee",
};
console.group("object loop");
for (var name in memberObject) {
console.log(name, memberObject[name]); // name은 변수이므로 []안에 넣어야함
}
console.groupEnd();
4.1 객체는 언제쓰나요?
console.log("Math.PI", Math.PI);
console.log("Math.random()", Math.random());
console.log("Math.floor(3.9)", Math.floor(3.9));
var MyMath = {
PI: Math.PI,
random: function () {
return Math.random();
},
floor: function (val) {
return Math.floor(val);
},
};
console.log("Mymath.PI", MyMath.PI);
console.log("Mymath.random()", MyMath.random());
console.log("Mymath.floor(3.9)", MyMath.floor(3.9));
// 객체를 안쓰면 각각 선언해줘야 하고 이름도 길어짐.
/* var MyMath_PI = Math.PI;
function MyMath_random() {
return Math.random();
}
function MyMath_floor(val) {
return Math.floor();
}
*/
5. this
var kim = {
name: "kim",
first: 10,
second: 20,
sum: function () {
/* return f + s; */
return this.first + this.second;
},
};
/* console.log("kim.first, kim.second", kim.sum(kim.first, kim.second)); */
console.log("kim.sum()", kim.sum());
6.1.constructor의 필요성
6.2.constructor의 사례
var d1 = new Date("2022-1-30");
console.log(d1.getFullYear());
console.log(d1.getMonth());
6.3. constructor 만들기
function Person(name, first, second, third) {
this.name = name;
this.first = first;
this.second = second;
this.third = third;
this.sum = function () {
return this.first + this.second + this.third;
};
}
var kim = new Person("kim", 10, 20, 30);
var lee = new Person("lee", 10, 10, 10);
console.log("kim.sum()", kim.sum());
console.log("lee.sum()", lee.sum());
7.2. prototype을 이용해서 재사용성을 높이기
프로토타입의 의미란?
프로토타입이 없을 떄의 비효율적인 점은 무엇인가?
프로토타입을 사용하면 좋은 점은 무엇인가?
8.2. Classes의 생성 ~ 10. 메소드 구현
class Person {
constructor(name, first, second) {
this.name = name;
this.first = first;
this.second = second;
}
sum() {
return "prototype: " + (this.first + this.second);
}
}
var kim = new Person("kim", 10, 20);
kim.sum = function () {
return "this : " + (this.first + this.second);
};
console.log(kim.sum());
var lee = new Person("lee", 10, 10, 10);
console.log(lee.sum());
11. 상속 (Inheritance)
class Person {
constructor(name, first, second) {
this.name = name;
this.first = first;
this.second = second;
}
sum() {
return (this.first + this.second);
}
}
class PersonPlus extends Person {
avg() {
return (this.first + this.second) / 2;
}
}
12. super
class Person {
constructor(name, first, second) {
this.name = name;
this.first = first;
this.second = second;
}
sum() {
return this.first + this.second;
}
}
class PersonPlus extends Person {
constructor(name, first, second, third) {
super(name, first, second);
this.third = third;
}
sum() {
return super.sum() + this.third;
}
avg() {
return (this.first + this.second + this.third) / 3;
}
}
var kim = new PersonPlus("kim", 10, 20, 56);
console.log("kim.sum()", kim.sum());
console.log("kim.avg()", kim.avg());