π»/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>