Last active
April 24, 2017 10:29
-
-
Save uyu423/fa27377b768f3d9c78ed3256f2efdc6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Money { | |
constructor(value) { | |
if (value < 0) throw Error('Money Type Valiation Exception'); | |
this.value = value; | |
} | |
} | |
class Email { | |
constructor(email) { | |
const regExp = /^[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,3}$/i; | |
if (!regExp.test(email)) throw Error('Email Type Validation Exception') | |
this.email = email; | |
Object.defineProperty(this, 'username', { | |
value: email.split('@')[0], | |
enumerable: false, | |
}); | |
Object.defineProperty(this, 'domain', { | |
value: email.split('@')[1], | |
enumerable: false, | |
}); | |
} | |
} | |
class User { | |
constructor(object) { | |
this.name = object.name; | |
this.credit = new Money(object.credit); | |
this.email = new Email(object.email); | |
} | |
} | |
const yowu = new User({ | |
name: 'Yowu', | |
credit: 10000, | |
email: '[email protected]', | |
}); | |
console.log(yowu); | |
// Real Output | |
// User { | |
// name: 'Yowu', | |
// credit: Money { value: 10000 }, | |
// email: Email { email: '[email protected]' } } | |
// 아래와 같이 출력되게 하는 방법이 있을까.. | |
// User { | |
// name: 'Yowu', | |
// credit: 10000, | |
// email: '[email protected]' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ungikim 사실 저렇게 해도 문제는 없는데 분명 더 아름답고 알아서 해주는 방법이 있을거 같아서리..