function createUser(){
this._email = undefined;
};
Object.defineProperty(
createUser.prototype,
"email",
{
configurable:false,
enumerable:false,
get:function(){
return this._email;
},
set:function(v){
if (!this._email) {
this._email = v;
} else {
console.log("Email has been set already!!");
}
}
}
)
Then:
var user = new createUser();
user.email; //undefined
user.email = "some@email.com";
user.email; //"some@email.com";
user.email = "some2@email.com"; //"Email has been set already!!"
user.email; //"some@email.com";
I wrote a plain old style js, so that others can use it ie9+. Do your custom sanitization etc inside the set handler (also make the own _email property a getter/setter that works with a salt enclosed in an iife, so no other can set them). There are many many other ways to achieve similar stuff, this is one of them.