classUser {constructor(firstName, lastName, age) {this.firstName = firstName;this.lastName = lastName;this.age = age; }getage() { //이해가 어렵 다시 찬찬히 읽어보기returnthis._age; }setage(value) {// if (value < 0) {// throw Error('age can not be negative');// }this._age = value <0?0: value; } //값을 설정하는 것이기 때문에 value를 가져와야함.}constuser1=newUser("steve","Job",-1);console.log(user1.age);
3. Fields (public, private)
// Too soon!// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fieldsclassExperiment { publicField =2; //외부에서 접근 가능 #privateField =0; //클래스 내부에서만 값이 보여지고 접근 가능}constexperiment=newExperiment();console.log(experiment.publicField);console.log(experiment.privateField);