본문 바로가기

Web/Javascript(ES6)

[ES6] getter / setter

Descriptor

var obj = {};
Object.defineProperty(obj, "book", {
    value : 123,
    enumerable: true
});


ES5 getter

var obj = {};
Object.defineProperty(obj, "book", {
    get: function(){
        return "책";
    }
});


ES6 getter

var obj = {
    value: 123,
    get getTotal(){
        return this.value;
    }
}


ES6 setter

var obj = {
    set setTotal(value){
        this.total = value;
    }
}
obj.setTotal = 123;


'Web > Javascript(ES6)' 카테고리의 다른 글

[ES6] for-of  (0) 2017.03.23
[ES6] Operation  (0) 2017.03.23
[ES6] Destructuring  (0) 2017.03.23
[ES6] Iteration  (0) 2017.03.23
[ES6] Lambda  (0) 2017.03.23