Reputation: 347
How does one create an object in Javascript conditional upon a certain condition inside said object. For instance:
function User(email) {
this.email = email;
this.checkValid = function () {
// check email if not valid delete the object or return nothing
}
this.checkValid()
}
var user1 = new User("[email protected]")
Upvotes: 0
Views: 99
Reputation: 566
You could use try
, catch
function User(email) {
this.email = email;
this.checkValid()
}
User.prototype.checkValid = function () {
var valid = false;
//or true if valid email
if(!valid) throw 'Not valid email';
}
try {
var user1 = new User("[email protected]");
} catch(e) {
console.log(e);
}
But in my opinion a constructor should always create an object, so I would do something like this:
function User(email) {
this.email = email;
}
User.prototype.isValid = function () {
if (this.email.match(/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/ig)) {
return true;
}
return false;
}
var user1 = new User("[email protected]");
if(!user1.isValid()){
user1 = null;
}
Upvotes: 0
Reputation: 13304
function createUser(username, email)
{
if (email.match(/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/ig))
{
window[username] = new User(email);
return true;
}
else
{
return null;
}
}
function User(email)
{
this.email = email;
}
if (createUser("user1", "[email protected]"))
{
document.write("User 1: " + user1.email + "<br />");
}
if (createUser("user2", "bob123aol.com"))
{
document.write("User 2: " + user2.email);
}
document.write(window['user1'] + "<br />");
document.write(window['user2']);
This will check if the user has a valid e-mail. If so create a global variable constructed from User
, if not nothing is returned. You can of course replace the window (global scope) object with any other object.
Upvotes: 1
Reputation: 664886
if not valid delete the object
Don't. Better, test the email address to be valid before trying to create the user.
or return nothing
You can't really. Returning nothing from a constructor is effectively quite impossible, except you throw an exception.
Use an extra factory function instead:
function isValidEmail(str) {
// http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/
return /.+@.+\..+/.test(str);
}
function User(email) {
// possible, but better don't do this:
// if (!isValidEmail(email)) throw new Error("Tried to create User with invalid email")
this.email = email;
}
User.prototype.checkValid = function () {
return isValidEmail(this.email);
};
User.create = function(email) {
if (isValidEmail(email))
return new User(email);
else
return null;
};
var user1 = User.create("[email protected]")
if (user1)
this.checkValid() // true
Upvotes: 2
Reputation:
function User(email) {
this.email = email;
this.check();
};
User.prototype.check = function() {
if (this.email.match(/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/ig)) {
console.log('Valid email');
} else {
console.log('Invalid email');
}
};
var user1 = new User("[email protected]");
Upvotes: 0