js的set和get

'use strict'

class Student {
    constructor(_name,age){
        this.name = _name;
        this.age = age;
    }
    set name(_name){
        this._name = 'aaa'+_name;
    };
    get name(){
        return this._name;
    };
    set age(a){
        this._age = a;
    }
    get age(){
        return this._age;
    }
}
var ss =new Student('yan',19);
new时候先执行constructor,然后发现this.name然后找到了set 后的name这个方法。
console.log(ss);

 

posted @ 2016-07-06 18:37  飘然离去  阅读(142)  评论(0编辑  收藏  举报