πŸ’»/Javascript

(50) [JavaScript] 6일 JavaScript 객체 - 클래슀

λ”°κΆˆ 2024. 4. 28. 12:29

 

 

 

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>