객체(Object)

최성진·2023년 3월 9일
0

Javascript

목록 보기
6/8

1. javascript 객체 생성 과정

  • 빈 객체의 생성
    -> 아무런 기능이 없는 상태의 빈 객체를 생성
    이 상태가 prototype 이다.
  • 가능한것 : 변수의 추가, 함수의 추가

2. 빈 객체 생성

<script>
  let people = {};
</script>

3. 변수의 추가

<script>
  // 객체안에 변수 추가(멤버변수, 프로퍼티)
  people.name = "자바학생"
  people.gender = "여자";
</script>
<script>
let grades = {
    'abc' : 10, "def" : 6, "ghr" : 80
};
</script>

4. 객체에 함수넣기

  • 매서드 안에서 객체 자원 활용하기
  • 객체 안에 포함된 메서드에서 다른 메서드를 호출하거나, 프로퍼티(멤버변수)를 활용하고자 하는 경우에는 this 키워드를 사용한다.
    this.변수이름;
<script>
객체이름.함수이름 = function(파라미터)){
    함수 구현부분
    return;
};
</script>
<script>
const people = {};
people.name = "자바학생";
people.gender = "남자";

// 함수를 포함시키기
people.sayName = function(){
    document.write("<h1>" + this.name + "</h1>" );  
};
  
people.sayGender = function(){
    document.write("<h1>" + this.gender + "</h1>" );
};

people.saySomething = function(msg){
    document.write("<h1>" + msg + "</h1>" );
};

people.getName = function(){
    return this.name;
};

people.getGender = function(){
    return this.gender;
};

people.sayInfo = function(){
    document.write("<h1>" + this.getName() + "님은 " + this.getGender() +  "입니다.</h1>" );
}

// 객체 안에 메서드 호출
    people.sayName();
    people.sayGender();
    people.saySomething("Hello javascript");
    people.sayInfo();
</script>
profile
마부리입니다

0개의 댓글