9033 / coding_programming

coding programming of problems
MIT License
0 stars 0 forks source link

ES6 (javascript) #8

Open 9033 opened 3 years ago

9033 commented 3 years ago

객체 동결

9033 commented 3 years ago

Assign Variables

const obj = {
  name: 'kim',
  age: 99,
  grade:{
    math: 'A',
    history: 'B'
  }
}

const {
  name = 'lee',
  age = 11,
  where = 'korea',
  grade: {
    math: mathGrade,
    history: historyGrade,
    science: scienceGrade = 'F'
  }
} = obj

console.log(name, age, where, mathGrade, historyGrade, scienceGrade)
9033 commented 3 years ago

Function's Parameters

const fn = ({x:y = '1', t:z = '2'} = {t:'3'}) => console.log(y, z)
fn()
fn({})
fn({x:'4'})
fn({t:'5'})
fn({x:'6', t:'7'})
const arrFn = ([a = 7,b = 8,c = 9, , ...etc] = [1, 2, 3]) => console.log(a, b, c, etc)
arrFn()
arrFn([])
arrFn([10])
arrFn([11, 12, 13, 14, 15, 16])
9033 commented 3 years ago

Getter / Setter

class MC {
  constructor(name){
    this._name = name;
  }
  get name(){
    return this._name;
  }
  set name(name){
    this._name = name;
  }
}

const mc = new MC('mc');

console.log(mc.name);
mc.name = 'mc one';
console.log(mc.name);
const temp = {
  1: null,
  _exist: false,
  get exist(){
    return this._exist
  },
  get w(){
    return this[1]
  },
  set w(val){
    this._exist = (val !== undefined && val !== null)
    this[1] = val
  }
}

console.log(temp.exist);
temp.w = 'mc two';
console.log(temp.exist);
console.log(temp.w);
const tempClass = new class MC2{
  constructor(){
    this['1'] = null
    this._exist = false
  }  
  get exist(){
    return this._exist
  }
  get w(){
    return this['1']
  }
  set w(val){
    this._exist = (val !== undefined && val !== null)
    this['1'] = val
  }
}

console.log(tempClass.exist);
tempClass.w = 'mc three';
console.log(tempClass.exist);
console.log(tempClass.w);