프로퍼티와 메소드

(Property & Method)

1. 메소드(Method)

객체에 소속된 함수

coworkers.showAll = function(){
    for(var key in this){
    document.write(key + ' : ' + this[key] + '<br>');
}
coworkers.showAll();

//변수명(coworkers)이 바뀌어도 영향받지 않기 위해 this로 리팩토링함

메소드(Method) = showAll

2. 프로퍼티(Property)

객체에 소속된 변수

var coworkers = {
    "programmer":"egoing",
    "designer":"leezche"
};
document.write(coworkers.programmer);        //egoing
document.write(coworkers.designer);          //leezche

coworkers.bookkeeper = "duru";
document.write(coworkers.bookkeeper);        //duru

coworkers["data scientist"] = "taeho";
document.write(coworkers["data scientist"]); //taeho

프로퍼티(Property) = programmer, designer, bookkeeper, data scientist

예시) 객체에 소속된 변수의 값으로 함수를 지정하기

<h2>Property &amp; Method</h2>
<script>
    var coworkers = {
        "programmer":"egoing",
        "designer":"leezche"
    };
    
    coworkers.showAll = function(){
        for(var key in this){
        document.write(key + ' : ' + this[key] + '<br>');
    }
}
coworkers.showAll();
</script>

전부 같은 코드

coworkers.showAll = function(){};

function showAll(coworkers){};

var showAll = function(coworkers){};

Last updated