'JavaScript object'에 해당되는 글 2건

반응형
// 문법을 엄격하게 봐줌
'use strict';


class Person {

    //생성자
    constructor(name, age){
        this.name = name;
        this.age = age;
    }

    //함수
    speak(){
        console.log(`${this.name}: hello`);

    }
}

const mario = new Person('Mario', 35);
console.log(mario.name);
console.log(mario.age);
mario.speak();


//2. Getter and setters
class User{
    constructor(firstName, lastName, age){
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
    get age(){
        return this._age;
    }

    set age(value){
        // if(value < 0){
        //     throw Error('age can not be negative');
        // }

        this._age = value < 0 ? 0 : value;
    }
}

const user1 = new User('Steave', 'job', -1);
console.log(user1.age);


//4. Static properties and methods
class Article {
    static publisher = 'Dream Coding';
    constructor(articleNumber) {
        this.articleNumber = articleNumber;
    }

    static printPublisher(){
        console.log(Article.publisher);
    }
}

const article1 = new Article(1);
const article2 = new Article(2);

console.log(Article.publisher);
Article.printPublisher();

//5. Inheritance
class Shape {
    constructor(width, height, color){
        this.width = width;
        this.height = height;
        this.color = color;
    }

    draw(){
        console.log(`drawing ${this.color} color of`);
    }

    getArea(){
        return this.width * this.height;
    }
}

class Rectangle extends Shape {

}
class Triangle extends Shape{
    draw(){
        super.draw();
        console.log(' :) ')
    }
    getArea(){
    return (this.width * this.height) / 2 ;
    }
    toString(){
        return `Triangle Color : ${this.color}`;
    } 
}

const rectangle = new Rectangle(20, 20, 'blue');
rectangle.draw();
console.log(rectangle.getArea());
const triangle = new Triangle(20, 20, 'red');
triangle.draw();
console.log(triangle.getArea()); 
console.log(triangle.toString());
반응형
블로그 이미지

꽃꽂이하는개발자

,
반응형

아래의 화면은 codesandbox로 실행했습니다.

 

 

const ironMan = {
  name: "토니 스타크",
  actor: "로버트 다우니 주니어",
  alias: "아이언 맨"
};

const captainAmerica = {
  name: "스티븐 로저스",
  actor: "크리스 에반스",
  alias: "캡틴 아메리카"
};

function print(hero) {
  const text = `${hero.alias}(${hero.name}) 역할을 맡은 배우는 ${
    hero.actor
  } 입니다.`;
  console.log(text);
}
function print1(hero) {
  const { alias, name, actor } = hero;
  const text = `${alias}(${name}) 역할을 맡은 배우는 ${actor} 입니다.`;
  console.log(text);
}

function print2({ alias, name, actor }) {
  const text = `${alias}(${name}) 역할을 맡은 배우는 ${actor} 입니다.`;
  console.log(text);
}

const { name } = ironMan;
console.log(name);

print(ironMan);
print(captainAmerica);

print1(ironMan);
print1(captainAmerica);

print2(ironMan);
print2(captainAmerica);

 

const deepObject = {
  state: {
    information: {
      name: "sh",
      language: ["korean", "english", "java"]
    }
  },
  value: 5
};

const { name, language } = deepObject.state.information;
const { value } = deepObject;

const extracted = { name, language, value };
console.log(extracted);
반응형
블로그 이미지

꽃꽂이하는개발자

,