JavaScript - The Complete Guide 2022

Section 12: Practice: Object-oriented Programming - Time to Practice!

olivia_yj 2022. 10. 31. 03:40

The goals

๐Ÿ’ช๐ŸปPractice Object-oriented Programming

 

static

The static keyword defines a static method or property for a class, or a class static initialization block (see the link for more information about this usage). Neither static methods nor static properties can be called on instances of the class. Instead, they're called on the class itself.

Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.

 

prototype์ด ์•„๋‹Œ ํด๋ž˜์Šค ํ•จ์ˆ˜ ์ž์ฒด์— ๋ฉ”์„œ๋“œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค
์ด๋Ÿฐ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์ (static) ๋ฉ”์„œ๋“œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค

class Person {
 	static staticMethod() {
     	console.log("static"); 
    }
}

Person.staticMethod(); // static

์ •์  ๋ฉ”์„œ๋“œ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ”„๋กœํผํ‹ฐ ํ˜•ํƒœ๋กœ ์ง์ ‘ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ์ผ์„ ํ•œ๋‹ค

class Person { }

Person.staticMethod = function() {
 	console.log("static"); 
}

Person.staticMethod(); // static

Person.staticMethod()๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ this์˜ ๊ฐ’์€ ํด๋ž˜์Šค ์ƒ์„ฑ์ž์ธ User ์ž์ฒด๊ฐ€ ๋œ๋‹ค (์  ์•ž ๊ฐ์ฒด)

์ •์  ๋ฉ”์„œ๋“œ๋Š” ์–ด๋–ค ํŠน์ •ํ•œ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค์— ์†ํ•œ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค

 

class Posting {
 	constructor(title, date) {
      	this.title = title;
      	this.date = date;
    }
  
  	static compareDate(postingA, postingB) {
     	return postingA.date - postingB.date; 
    }
}

let postings = [
 	new Posting("์ •์  ๋ฉ”์„œ๋“œ", new Date(2022, 12, 22)),
    new Posting("์€๋‹‰ํ™”", new Date(2022, 11, 28)),
    new Posting("๊ฐ์ฒด์ง€ํ–ฅ", new Date(2022, 05, 10)),
];

posting.sort(Posting.compareDate);

conole.log(postins[0].title); // ๊ฐ์ฒด์ง€ํ–ฅ

Posting.compareDate๋Š” posting์„ ๋น„๊ตํ•ด์ฃผ๋Š” ์ˆ˜๋‹จ์œผ๋กœ ๊ธ€ ์ „์ฒด๋ฅผ ์œ„์—์„œ ๋ฐ”๋กœ๋ณด๋ฉฐ ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค
Posting.compareDate๋Š” ๊ธ€ ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ์—ฌ์•ผ ํ•˜๋Š” ์ด์œ ์ด๋‹ค

 

ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ์ด๋‹ค

class Posting {
	constructor(title, date) {
     	this.title = title;
      	this.date = date;
    }
  
  	static createToday() {
      	// this๋Š” Posting์ด๋‹ค
    	return new this("Monday", new Date());
    }
}

let posting = Posting.createToday();
console.log(posting.title); // Monday

์ด์ œ Monday๋ผ๋Š” ๋ฌธ์ž๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค Posting.createToday()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค
์—ฌ๊ธฐ์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Posting.createToday()๋Š” posting ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹Œ ์ „์ฒด ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ์ด๋‹ค


์ •์  ๋ฉ”์„œ๋“œ๋Š” ํ•ญ๋ชฉ ๊ฒ€์ƒ‰, ์ €์žฅ, ์‚ญ์ œ, ์ˆ˜์ • ๋“ฑ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ๊ด€๋ จ ํด๋ž˜์Šค์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

Posting.remove({ id: 1001 })

 

์ •์  ํ”„๋กœํผํ‹ฐ

์ •์  ํ”„๋กœํผํ‹ฐ๋„ ์ผ๋ฐ˜ ํด๋ž˜์Šค ํ”„๋กœํผํ‹ฐ์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์•ž์— static์ด ๋ถ™๋Š”๋‹ค

class Posting {
 	static postA = "ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ"; 
}

console.log(Posting.postA); // ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ

Posting์— ํ”„๋กœํผํ‹ฐ๋ฅผ ์ง์ ‘ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค

Posting.postA = "ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ";

 

์ •์  ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ ์ƒ์†

์ •์  ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋Š” ์ƒ์†์ด๋œ๋‹ค

class Human {
 	static korean = "ํ•œ๊ตญ์ธ";

	constructor(name, speed) {
      	this.name = name;
      	this.speed = speed;
    }

	run(speed = 0) {
     	this.speed += speed;
      	console.log(`${this.name}์ด ${this.speed}์†๋„๋กœ ๋‹ฌ๋ฆฐ๋‹ค`);
    }

	static compare(humanA, humanB) {
    	return humanA.speed - humanB.speed;
    }
}

class SoccerPlayer extends Human {
 	goal() {
     	console.log(`${this.name}์ด ๊ณจ์„ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค!`);
    }
}

let soccerPlayer = [
	new SoccerPlayer("์†ํฅ๋ฏผ", 30);
  	new SoccerPlayer("์ผ€์ธ", 25);
];

soccerPlayer.sort(SoccerPlayer.compare);

soccerPlayer[0].run(); // ์ผ€์ธ์ด 25์†๋„๋กœ ๋‹ฌ๋ฆฐ๋‹ค

console.log(SoccerPlayer.korean); // ํ•œ๊ตญ์ธ

SoccerPlayer.compare๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด Human.compare๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค
์ด๊ฒŒ ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” extendํ‚ค์›Œ๋“œ๋Š” SoccerPlayer์˜ [[Prototype]]์ด Human์„ ์ฐธ์กฐํ•˜๊ฒŒ ํ•ด์ค€๋‹ค

๋”ฐ๋ผ์„œ SoccerPlay extends Human์€ ๋‘๊ฐœ์˜ [[Prototype]] ์ฐธ์กฐ๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค

  • ํ•จ์ˆ˜ SoccerPlayer๋Š” ํฌ๋กœํ† ํƒ€์ž…์„ ํ†ตํ•ด ํ•จ์ˆ˜ Human์„ ์ƒ์†๋ฐ›๋Š”๋‹ค
  • SoccerPlayer.prototype์€ ํ”„๋กœํ† ํƒ€์ž…์„ ํ†ตํ•ด Human.prototype์„ ์ƒ์†๋ฐ›๋Š”๋‹ค

์ด๋Ÿฐ ๊ณผ์ •์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ ์ƒ์†๊ณผ ์ •์  ๋ฉ”์„œ๋“œ ์ƒ์†์ด ๊ฐ€๋Šฅํ•˜๋‹ค

class Human {}
class SoccerPlayer extends Human {}

// ์ •์  ๋ฉ”์„œ๋“œ
console.log(SoccerPlayer.__proto__ === Human); // true

// ์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ
console.log(SoccerPlayer.__proto__ === Human.prototype); // true

 

์ •๋ฆฌ

์ •์  ๋ฉ”์„œ๋“œ๋Š” ํŠน์ • ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค ์ „์ฒด์— ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

์ธ์Šคํ„ด์Šค๋ผ๋ฆฌ ๋น„๊ตํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ Posting.compareDate(postingA, postingB)์ด๋‚˜ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ Posting.createToday()๋ฅผ ๋งŒ๋“ค ๋•Œ ์ •์  ๋ฉ”์„œ๋“œ๊ฐ€ ์“ฐ์ธ๋‹ค

์ •์  ๋ฉ”์„œ๋“œ๋Š” ํด๋ž˜์Šค ์„ ์–ธ๋ถ€ ์•ˆ์— ์œ„์ฐจํ•˜๊ณ  ์•ž์— static์ด๋ผ๋Š” ํ‚ค์›Œ๋“œ๊ฐ€ ๋ถ™๋Š”๋‹ค

์ •์  ํ”„๋กœํผํ‹ฐ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํด๋ž˜์Šค ์ˆ˜์ค€์— ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค
์ •์  ํ”„๋กœํผํ‹ฐ ์—ญ์‹œ ๊ฐœ๋ณ„ ์ธ์Šคํ„ด์— ๋ฌถ์ด์ง€ ์•Š๋Š”๋‹ค

class Class {
 	static property = "property";

	static method() {
    }
}
// ์œ„์™€ ๊ฐ™๋‹ค
Class.property = "property"'
Class.method = function() { }

์ •์  ํ”„๋กœํผํ‹ฐ์™€ ์ •์  ๋ฉ”์„œ๋“œ๋Š” ์ƒ์†์ด ๊ฐ€๋Šฅํ•˜๋‹ค
class B extends A๋Š” ํด๋ž˜์Šค B์˜ ํ”„๋กœํ† ํƒ€์ž…์ด ํด๋ž˜์Šค A๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ํ•œ๋‹ค (B.[[Prototype]] = A)
๋”ฐ๋ผ์„œ B๊ฐ€ ์›ํ•˜๋Š” ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐพ์ง€ ๋ชปํ•˜๋ฉด A๋กœ ๊ฒ€์ƒ‰์ด ์ด์–ด์ง„๋‹ค

 

 

Sources

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static

 

static - JavaScript | MDN

The static keyword defines a static method or property for a class, or a class static initialization block (see the link for more information about this usage). Neither static methods nor static properties can be called on instances of the class. Instead,

developer.mozilla.org

https://ko.javascript.info/static-properties-methods

 

์ •์  ๋ฉ”์„œ๋“œ์™€ ์ •์  ํ”„๋กœํผํ‹ฐ

 

ko.javascript.info

https://velog.io/@giriboy/%EC%A0%95%EC%A0%81-%EB%A9%94%EC%84%9C%EB%93%9C%EC%99%80-%EC%A0%95%EC%A0%81-%ED%94%84%EB%A1%9C%ED%8D%BC%ED%8B%B0

 

์ •์  ๋ฉ”์„œ๋“œ์™€ ์ •์  ํ”„๋กœํผํ‹ฐ

prototype์ด ์•„๋‹Œ ํด๋ž˜์Šค ํ•จ์ˆ˜ ์ž์ฒด์— ๋ฉ”์„œ๋“œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค์ด๋Ÿฐ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์ (static) ๋ฉ”์„œ๋“œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค

velog.io