달나라 노트

Google Apps Script : static attribute, static method, static member, 정적 속성, 정적 메서드, 정적 멤버 본문

Google Apps Script

Google Apps Script : static attribute, static method, static member, 정적 속성, 정적 메서드, 정적 멤버

CosmosProject 2022. 11. 29. 00:06
728x90
반응형

 

 

 

보통 class에 있는 attribute나 method는 다른 객체로 만든 채로 사용할 수 있습니다.

class 자체로부터 method를 호출하거나 할 수 없습니다.

 

class Cat {
  constructor(name, age, weight, color) {
    this.name = name;
    this.age = age;
    this.weight = weight;
    this.color = color;
    this.species = 'mammal';
  }

  sound() {
    Logger.log('Moew~');
  }
}

function myFunction(){
  var my_cat = new Cat(name='Kitty', age=5, weight=4.5, color='white');

  my_cat.sound();
}



-- Result
Moew~

 

위 예시를 보면 Cat class를 만들고

Cat class를 이용해 my_cat을 인스턴스화(=객체화)하여 my_cat을 객체로 만든 후,

my_cat.sound()와 같이 객체인 my_cat으로부터 sound() method를 호출하고 있습니다.

 

 

 

class Cat {
  constructor(name, age, weight, color) {
    this.name = name;
    this.age = age;
    this.weight = weight;
    this.color = color;
    this.species = 'mammal';
  }

  sound() {
    Logger.log('Moew~');
  }
}

function myFunction(){
  Cat.sound();
}



-- Result
TypeError: Cat.sound is not a function

 

위 코드처럼 Cat.sound() 와 같은 방식으로 class 자체로부터 class에 있는 method를 실행시키는 것은 불가합니다.

 

 

 

 

 

 

class Cat {
  constructor(name, age, weight, color) {
    this.name = name;
    this.age = age;
    this.weight = weight;
    this.color = color;
    this.species = 'mammal';
  }

  sound() {
    Logger.log('Moew~');
  }
}

function myFunction(){
  Logger.log(Cat.species);
}



-- Result
null

 

이러한 방식은 attribute에도 적용됩니다.

위 예시를 보면 class인 Cat으로부터 직접 species라는 속성을 불러내고 있습니다.

 

Cat class에선 분명히 this.species = 'mammal'이라는 부분으로부터 species라는 속성은 mammal이라는 값을 가지도록 코딩되어 있습니다.

그러나 그 결과는 null이죠.

 

이처럼 기본적으로 class에 있는 정보는 직접적으로 불러올 수 없습니다.

반드시 class를 이용하여 어떠한 객체를 만들고 그 객체로부터 attribute, method 들을 호출해야 하죠.

 

 

 

하지만 class로부터 직접적으로 attribute 또는 method를 호출할 수 있는 방법이 있습니다.

이렇게 class 자체로부터 가져올 수 있는 attribute를 static attirubte(= 정적 속성)이라고 합니다.

class 자체로부터 가져올 수 있는 method를 static method(= 정적 메서드)라고 합니다.

그리고 이 둘을 합쳐서 static member(= 정적 멤버)라고 합니다.

 

 

이제 이것들을 어떻게 만들고 사용할 수 있는지 알아봅시다.

 

 

 

 

class Cat {
  constructor(name, age, weight, color) {
    this.name = name;
    this.age = age;
    this.weight = weight;
    this.color = color;
    this.species = 'mammal';
  }

  static sound() {
    Logger.log('Moew~');
  }
}

function myFunction(){
  Logger.log(Cat.sound());
}



-- Result
Meow~

 

먼저 static method 관련 내용입니다.

 

위 코드를 보면 myFunction() 부분에서 Cat class를 다른 변수에 할당하여 객체화하지 않고도 sound method를 호출한 것을 볼 수 있죠.

 

 

  static sound() {
    Logger.log('Moew~');
  }

 

static method는 매우 쉽습니다.

 

위 부분을 보면 Cat class에서 sound() method를 설정할 때 sound 왼쪽에 static이라는 키워드를 붙여준 걸 볼 수 있습니다.

이렇게 static 키워드를 method 앞에 붙여주면 이 method는 static method라고 설정해주는 의미가 됩니다.

 

따라서 sound method는 static method가 되어버렸고,

Cat class를 다른 변수에 할당하여 객체를 생성하기도 전에 Cat class 자체로부터 sound method를 호출할 수 있게 되는 것이죠.

 

 

 

 

 

 

 

 

 

 

 

 

 

class Cat {
  constructor(name, age, weight, color) {
    this.name = name;
    this.age = age;
    this.weight = weight;
    this.color = color;
    this.species = 'mammal';
  }

  static sound() {
    Logger.log('Moew~');
  }
}

function myFunction(){
  Cat.eye_color = 'black';

  Logger.log(Cat.eye_color);
}



-- Result
black

 

이번에는 static attribute 관련 내용입니다.

 

위 코드를 보면 eye_color라는 속성을 Cat class에 추가하였고,

그 후에 Cat class로부터 eye_color라는 attribute를 불러왔을 때 black이라는 글자가 제대로 출력되는 것을 볼 수 있죠.

 

 

  Cat.eye_color = 'black';

  Logger.log(Cat.eye_color);

 

static attribute를 만드는 방법은 바로 위 부분에 있습니다.

eye_color라는 attribute를 static attribute로 만들려면 위처럼 Cat class를 모두 만들어 두고 다른 부분에서 Cat class에 새로운 attribute를 추가하는 듯한 방식으로 하면 됩니다.

 

 

class Cat {
  constructor(name, age, weight, color) {
    this.name = name;
    this.age = age;
    this.weight = weight;
    this.color = color;
    this.species = 'mammal';
  }

  static sound() {
    Logger.log('Moew~');
  }
}

function myFunction(){
  Cat.eye_color = 'black';

  Logger.log(Cat.eye_color);
}

 

그래서 위 코드를 보면 Cat class 선언 부분에는 eye_color라는 속성이 어디에도 없습니다.

 

- Cat.eye_color = 'black';

하지만 myFunction 부분에서 위처럼 Cat class에 eye_color라는 attribute를 추가해주는 것이죠.

 

이런 방식으로 추가된 attribute는 static attribute로 간주되며,

객체를 만들지 않고도 class 자체로부터 호출할 수 있게 됩니다.

 

 

 

 

 

 

728x90
반응형
Comments