FrontEND/JavaScript

public, private, get,set

smartlittlepuppy 2023. 4. 16. 19:11
반응형
// 1. Array
const array = [1, 2, 3];
const push1 = array.push(4);

console.log(array);

/* 2. New Construction
this는 CreateProduct 함수를 호출하는 시점에 생성된 객체를 가리킵니다.
따라서, this.radius와 this.color는 각 객체의 radius와 color 속성에 값을 할당하게 됩니다.
 this.discount는 Product 객체에 discount 메서드를 추가하고, 해당 메서드를 객체의 속성으로 등록합니다.
*/
function CreateProduct(title, price) {
  let store = "1000"; //private
  //private된 변수를 보이게하기 (Get)
  this.get = function () {
    return store;
  };
  //(Set)
  this.set = function (value) {
    return (store = value);
  };

  this.title = title; //public
  this.price = price; //public
  this.discount = function () {
    console.log("discount");
    console.log(store);
  };
}

const product1 = new CreateProduct("milk", 5);
console.log(product1);
console.log(product1.title);
console.log(product1.get());
console.log(product1.set(2000));
반응형

'FrontEND > JavaScript' 카테고리의 다른 글

class 문법을 사용하면 상속 관계  (0) 2023.04.16
프로토타입 기반의 상속 방법  (0) 2023.04.16
프로토타입  (0) 2023.04.16
pixabay API  (0) 2023.04.13
vscode와 github Issues만들기  (0) 2023.04.10