ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Class vs Object
    프로그래밍/Js 2020. 8. 3. 20:24

      javascript는 프로토타입 기반으로 객체 생성을 지원하는 동적 스크립트 언어이다.

     

    * 프로토타입 : 달리 클래스를 명확히 정의하지 않아도 되며, 속성과 메서드를 다른 클래스의 인스턴스나 빈 객체에 추가하는 작업을 덜 수 있는 프로그래밍 스타일

     

     1.class

          template

          declare once

          no data in

          ex) 붕어빵틀

     

     2.class

         instance of a class

         created many times

         data in 

          ex) 크림 붕어빵, 팥 붕어빵

     

     3. class declarations

    class Person {

        constructor(nameage) {

            this.name = name;

            this.age = age;

        }

     

        speak() {

            console.log(`${this.name} : hello!`);

        }

    }

     

    const person = new Person('왕별'20);

    console.log(person.name);

    console.log(person.age);

    ellie.speak();

     

     

     4. Getter and Setters

    class User {

        constructor(firstNamelastNameage) {

            this.firstName = firstName;

            this.lastName = lastName;

            this.age = age;

        }

     

        get age() {

            return this._age;

        }

     

        set age(value) {

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

        }

    }

    '프로그래밍 > Js' 카테고리의 다른 글

    Javascript-타입  (0) 2021.02.12
    JS-this, call, apply, bind  (0) 2021.01.17
    JS-closure  (0) 2021.01.15
    JS-arguments, Rest parameters, Spread syntax  (0) 2021.01.14
    Js-구조분해 할당  (0) 2021.01.14
Designed by Tistory.