JAVASCRIPT

자바스크립트 객체 지향 프로그램

Made Project 2020. 10. 25. 21:53
let grades = {
'list':{'egoing':10, 'k9905':8, 'sorialgo':80},
'show':function(){
  alert('this.list')
}
};

grades['show']()​
let grades = {
'list':{'egoing':10, 'k9905':8, 'sorialgo':80}
};

console.log(grades['list'])

{ egoing: 10, k9905: 8, sorialgo: 80 } 이렇게 나옵니다.

 

let grades = {
'list':{'egoing':10, 'k9905':8, 'sorialgo':80}
};

console.log(grades['list']['egoing']);

 

10이 나옵니다.

 

 

 

객체 안에 함수를 실행할 때 예를 들자면 

 

let grades = {
'list':{'egoing':10, 'k9905':8, 'sorialgo':80},
'show':function(){
  console.log('helow world')
}
};

grades['show']()

hellow world뜹니다.

 

 

값으로서의 함수도 객체에 넣을수 있습니다.

 

 

let grades = {
'list':{'egoing':10, 'k9905':8, 'sorialgo':80},
'show':function(){
  alert(this)
}
};

grades['show']()

 

여기서 this 약속되어있는 변수이고 show value 값 함수를 담고 있는 grades를 가르키는 변수입니다.

 

 

let grades = {
'list':{'egoing':10, 'k9905':8, 'sorialgo':80},
'show':function(){
  console.log(this.list)
}
};

grades['show']()

결과값은 'list':{'egoing':10, 'k9905':8, 'sorialgo':80}

 

 

let grades = {
  'list':{'egoing':10, 'k9905':8, 'sorialgo':80},
  'show':function(){
   for(let name in this.list){
   console.log(name, this.list[name])
   }
 }
}

grades.show();

'egoing' 10
'k9905' 8
'sorialgo' 80

 

결과값이 나옵니다.

 

grades 안에는 크게 list ,show 두가지로 그룹핑 되어있습니다.

 

바로 이런것들을 객체지향 프로그래밍이라고 합니다.

 

서로 연관되어있는 데이터와 처리를 하나의 그릇안에 그룹핑 해놓은 것이다.

 

 

이포스팅은 생활코딩을 참고해서 만들었습니다. egoing님 감사합니다.