๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป/Javascript

(50) [JavaScript] 6์ผ JavaScript ๊ฐ์ฒด - ํด๋ž˜์Šค

by ๋”ฐ๊ถˆ 2024. 4. 28.

 

 

 

Class 
๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ํ…œํ”Œ๋ฆฟ์ด๋‹ค.
ํด๋ž˜์Šค๋Š” ๋ฐ์ดํ„ฐ์™€ ์ด๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜๋กœ ์ถ”์ƒํ™”ํ•œ๋‹ค.

 

 

 

 

 

</head>
<body>

	<h3>ํด๋ž˜์Šค</h3>

	<script type="text/javascript">
	class User{
		constructor({name,age}){ // ์ƒ์„ฑ์ž๋Š” constructor ํ‚ค์›Œ๋“œ๋กœ ์ •์˜  
			this.name=name; // ํ•„๋“œ
			this.age=age;
		}
		
		sayHello(){ // ๋ฉ”์†Œ๋“œ 
			return `์ด๋ฆ„:${this.name},๋‚˜์ด:${this.age}`; 
		}
	}
	
	var obj={name:'ํ™๊ธธ๋™',age:20};
	
	const user = new User(obj);
	console.log(user.name);
	
	var s = user.sayHello();
	console.log(s);
	
	console.log(typeof User); // function
	console.log(user instanceof User); // true 
	
	</script>

</body>
</html>

 

 

</head>
<body>

	<h3>ํด๋ž˜์Šค - ๋ฉ”์†Œ๋“œ</h3>

	<script type="text/javascript">
	class Calculator {
		add(x,y){
			return x+y;
		}
		
		sum(x,y){
			return x-y;
		}
		
	}
	
	var obj = new Calculator();
	console.log(obj.add(10,5));
	
	const methodName ='sayName';
	class User{
		constructor({name,age}){
			this.name=name;
			this.age=age;
		}
		
		[methodName](){ // ์ž„์˜์˜ ํ‘œํ˜„์‹์œผ๋กœ ๋ฉ”์†Œ๋“œ๋ช… ๋ถ€์—ฌ 
			return `${this.name}:${this.age}`;
		}
	}
	
	var obj2=new User({name:'ํ•˜ํ•˜ํ•˜',age:17});
	console.log(obj2.sayName());
	
	
	
	
	</script>



</body>
</html>

 

 

</head>
<body>

	<h3>ํด๋ž˜์Šค : getter / setter</h3>

	<script type="text/javascript">
	class User{
		constructor(){
			this._name='';
		}
		
		get name(){
			return this._name;
		}
		set name(newName){
			this._name=newName;
		}
	}
	const obj = new User();
	//obj._name='ํ˜ธํ˜ธํ˜ธ';
	//console.log(obj._name);
	
	obj.name = 'ํ˜ธํ˜ธํ˜ธ';
	console.log(obj.name);
	console.log(obj._name);
	console.log('--------------------------')
	
	class Rect{
		constructor(w,h){
			this.w =w;
			this.h = h;
			
		}
		
		calcArea(){
			return this.w*this.h;
		}
		
		//getter
		get area(){
			return this.calcArea();
			
		}
	}
	
	const rect = new Rect(10,5);
	//let a = rect.calcArea();
	let a = rect.area;
	console.log(a);
	
	</script>

</body>
</html>

 

 

 

</head>
<body>


	<h3>ํด๋ž˜์Šค/์ธ์Šคํ„ด์Šค ์†์„ฑ</h3>

	<script type="text/javascript">
	/*
class Rect{
	constructor(w,h){
		this.w = w;
		this.h = h;
		
	}
}

var obj = new Rect(10,5);
console.log(obj.w,obj.h);
*/

/*class Rect{
	constructor(w,h){ // ์ƒ์„ฑ์ž
		this.w = 10; 
		this.h = 5;
		
	}
}

var obj = new Rect(0);
console.log(obj.w,obj.h); // 10 5 
*/

class Rect{
	//constructor(){}
area(w,h){ // ๋ฉ”์†Œ๋“œ 
	this.w = w;
	this.h = h;
	return this.w*this.h;
	
}
}

var obj = new Rect();
console.log(obj.w,obj.h); // undefined undefined

var s = obj.area(10,5);
console.log(obj.w,obj.h,s); // 10 5 50

</script>


</body>
</html>

 

 

</head>
<body>

	<h3>ํด๋ž˜์Šค - static ๋ฉ”์†Œ๋“œ</h3>


	<script type="text/javascript">
	class Rect {
		construtor(w,h){
			this.w=w;
			this.h=h;
		}
		
		static displayName='Rectangle'; // static ์†์„ฑ
		static area(obj){ // static ๋ฉ”์†Œ๋“œ 
			return obj.w*obj.h;
		}
	}
	
	
	var obj = new Rect (10,5);
	var s = Rect.area(obj);
	
	console.log(Rect.displayName,s);
	
	</script>
</body>
</html>

 

 

</head>
<body>

	<h3>class-์ƒ์†</h3>


	<script type="text/javascript">
	class Car{
		constructor(name){
			this.name=name;
		}
		speed(){
			console.log(`${this.name}๋Š” ์‹œ์† 80์ž…๋‹ˆ๋‹ค`);
		}
		color(){
			console.log(`${this.name}๋Š” ๊ฒ€์ •์ƒ‰์ž…๋‹ˆ๋‹ค.`);
		}
	}
	
	class Taxi extends Car{
		constructor(name){
			super(name); // ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ 
		}
		speed(){
			super.speed();
			console.log(`${this.name}๋Š” ์‹œ์† 100์ž…๋‹ˆ๋‹ค.`);
		}
	}
	
	var obj = new Taxi('๊ทธ๋žœ์ €');
	console.log(obj.speed());
	console.log(obj.color());
	
	</script>
</body>
</html>

 

 

</head>
<body>

	<h3>ํด๋ž˜์Šค-private</h3>

	<script type="text/javascript">
	class User{
		static #A; //private 
		#B;
		
		constructor(){
			this.#B=100;
		}
		
		static fun(){
			User.#A=10;
			return User.#A;
		}
		
		sub(){
			return this.#B;
		}
		
		#method(){//private 
			return 'hello...';
		}
		
		msg(){
			return this.#method();
		}
	}
	
	const obj = new User();
	
	//console.log(User.#A);
	console.log(User.fun());
	console.log(obj.sub());
	console.log(obj.msg());
	
	</script>

</body>
</html>