Javascript - Class ยท Object
< Class >
- object(์ธ์คํด์ค)๋ฅผ ๋ง๋ค ์ ์๋ ํ์ด๋ค.
- template(ํ๋ง์ ์ ์ํด์ ํ๋ฒ๋ง ์ ์ธํ๋ค.)
- class ์์ฒด์ data๊ฐ ๋ค์ด์์ง ์๋ค.
- class๋ ์ ์๋ง ํ ๊ฒ์ด๋ผ ์ค์ ๋ฉ๋ชจ๋ฆฌ์๋ ์ฌ๋ผ๊ฐ์ง ์๋๋ค.
1. Class ์ ์ธ
class Person {
//constructor (์์ฑ์)
constructor(name, age) {
//fields
this.name = name;
this.age = age;
}
//methods
speak() {
console.log(`${this.name}: hello!`);
}
}
const hyesung = new Person("hyesung", 24);
console.log(hyesung.name);
console.log(hyesung.age);
hyesung.speak();

2. Getter and Setters
๐ก ์ฌ์ฉ์๊ฐ class๋ฅผ ์๋ชป ์ฌ์ฉํด๋ ๋ฐฉ์ด์ ์ธ ์์ธ๋ก ๋ง๋ค์ ์๋๋ก ํด์ฃผ๋ ๊ฒ
class User {
constructor(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age; //getter์ setter๊ฐ ์ ์๋์ด ์๋ ๊ฒฝ์ฐ ์๋์ผ๋ก getter,setter ํธ์ถ
}
get age() {
return this._age; //๊ธฐ์กด์ age์ฌ์ฉ์ ๋ฌดํํธ์ถ
}
set age(value) {
this._age = value < 0 ? 0 : value; // ์ฌ์ฉ์์ ์
๋ ฅ๊ฐ ๊ฒ์ฌ
}
}
const use1 = new User("Steve", "Job", -1); //๋์ด๋ฅผ -1๋ก ์๋ชป ์ค์ ์
console.log(use1.age);
3. Inheritance (์์)
- ํ ํด๋์ค์์ ๋ค๋ฅธ ํด๋์ค๋ก ํ์ฅํ๊ณ ์ ํ ๋
- ๊ณตํต๋์ด์ง๋ ๊ฐ๋ค์ extends ํค์๋๋ก ์ฌ์ฌ์ฉ ํ ์ ์๋ค
4. Polymorphism(๋คํ์ฑ)
- ํ์ํ ๋ฉ์๋๋ฅผ ์ฌ์ ์ํด์ ์ฌ์ฉํ ์ ์๋ค(์ค๋ฒ๋ผ์ด๋ฉ)
- ์ค๋ฒ๋ผ์ด๋ฉ ํ ์ํ์์ ๋ถ๋ชจ class์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ผ๋ฉด super ํค์๋๋ฅผ ์ฌ์ฉ
class Shape {
constructor(width, height, color) {
this.width = width;
this.height = height;
this.color = color;
}
draw() {
console.log(`drawing ${this.color} color!`);
}
getArea() {
return this.width * this.height;
}
}
class Rectangle extends Shape {} //์์์ ํตํด ์ ์ํ๋ ๋ฐฉ๋ฒ
class Triangle extends Shape {
draw() {
super.draw(); //super : ์ฌ์ ์ ์, ๋ถ๋ชจ์ method๊น์ง ํธ์ถ
console.log("๐บ");
}
//๋คํ์ฑ : ํ์ํ ํน์ ํจ์๋ง ์ฌ์ ์
getArea() {
return (this.width * this.height) / 2;
}
}
const rectangle = new Rectangle(20, 20, `Green`);
rectangle.draw();
console.log(rectangle.getArea());
const triangle = new Triangle(20, 20, `Blue`);
triangle.draw();
console.log(triangle.getArea());

5. Class cheking: instanceOf
- instanceof ๋ฅผ ๊ธฐ์ค์ผ๋ก ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ ๋น๊ต
console.log(rectangle instanceof Rectangle); //True
console.log(triangle instanceof Rectangle); //False
console.log(triangle instanceof Triangle); //True
console.log(triangle instanceof Shape); //True
console.log(triangle instanceof Object); //True

< Object >
- object = { key : value }
- ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฐ์ฒด๋ key์ value๋ก ๊ตฌ์ฑ๋ property์ ์งํฉ
- key๋ ์ ๊ทผํ ์ ์๋ ๋ณ์์ด๊ณ , value๋ ๊ทธ์ ํด๋นํ๋ ๊ฐ
1. Literals and properties
- โobject literalโ syntax : ๋ณ์๋ฅผ ์ง์ ํ์ฌ object์์ฑ
const obj1 = {};
-โobject constructorโ syntax : ํด๋์ค๋ฅผ ์ด์ฉํ์ฌ object ์์ฑ
const obj2 = new Object();
//object๋ฅผ ์ฌ์ฉํ์ง ์์ ๊ฒฝ์ฐ,
const name = "hyesung";
const age = 25;
print(name, age);
function print(name, age) {
console.log(name);
console.log(age);
}
//object๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ,
const hyesung = { name: "hyesung", age: 25 };
function print1(person) {
console.log(person.name);
console.log(person.age);
}
print(hyesung);
2.Computed(๊ณ์ฐ๋) properties
- ๋์ ์ผ๋ก key, value๋ฅผ ๋ฐ์์์ผ ํ ๋ ์ฌ์ฉ
- ๋ณ์์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์ .๋ง๊ณ ๋ ๋ฐฐ์ด์์ ๋ฐ์ดํฐ ๋ฐ์์ค๋ฏ []์ฌ์ฉ๊ฐ๋ฅ
- ๋จ, [] ์ฌ์ฉ์ string์ผ๋ก ํํ
console.log(hyesung.name); //์ฝ๋ฉํ๋ ์๊ฐ key์ ํด๋นํ๋ ๊ฐ์ ๋ฐ์์ค๊ณ ์ถ์ ๋
console.log(hyesung["name"]); //์ ํํ ์ด๋ค key๊ฐ ํ์ํ์ง ๋ชจ๋ฅผ ๋(runtime์์ ๊ฒฐ์ ๋ ๋)
hyesung["hasJob"] = false;
console.log(hyesung.hasJob);
function printValue(obj, key) {
// console.log(obj.key); ํค ๊ฐ์ด ์ค๋ธ์ ํธ์ ์ ์ x
console.log(obj[key]); //์ง์ ๋์ง ์์ ์ค๋ธ์ ํธ๋ฅผ key๊ฐ ํํ
}
printValue(hyesung, "name");
printValue(hyesung, "age");
3. Property value shorthand (๋จ์ถ ์์ฑ๋ช )
- makePerson์ด๋ผ๋ ํจ์๋ฅผ ๋ง๋ค์ด์ ๊ฐ๋ง ์ ๋ฌํ๋ฉด object๋ฅผ ๊ฐ๋จํ ์์ฑ๊ฐ๋ฅ
const person1 = { name: "bob", age: 2 };
const person2 = { name: "steve", age: 3 };
const person3 = { name: "dave", age: 4 };
const person4 = makePerson("john", 30);
console.log(person4);
function makePerson(name, age) {
return {
name, //key์ value์ ์ด๋ฆ์ด ๋์ผํ๋ค๋ฉด ์๋ต๊ฐ๋ฅ
age,
};
}
4. Constructor Function (์์ฑ์ ํจ์)
function Person(name, age) {
//this = {}; ํํ์ ์๋ต
this.name = name;
this.age = age;
//return this; ํํ์ ์๋ต
}
- ์์ํ๊ฒ ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ๋ ํจ์๋ ๋๋ฌธ์๋ก ์์ํ๊ณ return ์ฐ์ง ์๊ณ class์์ constructor ํ๋ ๊ฒ์ฒ๋ผ this., ํธ์ถ์ new Person ์ฒ๋ผ ํ๋ค.
5. in operator : property existence check (key in obj) ํด๋นํ๋ object ์์ key ์ ๋ฌด
console.log("name" in hyesung);
console.log("age" in hyesung);
console.log("random" in hyesung);
console.log(hyesung.random);

6. for..in vs for..of
- for (key in obj)
for (let key in hyesung) {
console.log(key);
}
- for (value of iterable) ๋ฐฐ์ด, ๋ฆฌ์คํธ์์ ์์ฐจ์ ์ผ๋ก ๋ฐ์ดํฐ๊ฐ ๋ด๊ฒจ์๋ ๊ฒ์์ ์ฌ์ฉ
//์ด์
for(let i = 0; i < array.lenght; i++) {
console.log(array[i];);
}
// for of ์ฌ์ฉ
const array = [1, 2, 4, 5];
for(let value of array) {
console.log(value);
}
7. Object.assign(dest, [obj1, obj2, obj3โฆ])
- object ์์ฒด๋ฅผ ๋ณต์ฌ
const user = { name: "popo", age: 5 };
const use2 = user;
use2.name = "coder";
console.log(user);
//๋ฐฉ๋ฒ1
const user4 = {};
Object.assign(user4, user);
console.log(user4);
//๋ฐฉ๋ฒ2
const user5 = Object.assign({}, user);
console.log(user5);

์ถ์ฒ : ๋๋ฆผ์ฝ๋ฉ by ์๋ฆฌ https://youtu.be/_DLhUBWsRtw https://youtu.be/1Lbr29tzAA8