Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在特定条件下创建对象_Javascript_Constructor_Conditional Statements - Fatal编程技术网

Javascript 在特定条件下创建对象

Javascript 在特定条件下创建对象,javascript,constructor,conditional-statements,Javascript,Constructor,Conditional Statements,如何在Javascript中根据对象内部的特定条件创建对象。例如: 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("bob123@aol.com")

如何在Javascript中根据对象内部的特定条件创建对象。例如:

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("bob123@aol.com")
函数createUser(用户名、电子邮件)
{
如果(email.match(/^[a-z0-9!#$%&'*+\/=?^ `{124}-]+(?:\.[a-z0-9!#$%&'*+\/=?^ `{124}-]+)*([a-z0-9](?:[a-z0-9-]*[a-z0-9])+[a-z0
{
窗口[用户名]=新用户(电子邮件);
返回true;
}
其他的
{
返回null;
}
}
功能用户(电子邮件)
{
this.email=电子邮件;
}
如果(createUser(“user1”)bob123@aol.com"))
{
文件。写入(“用户1:+user1.email+”
); } if(createUser(“user2”,“bob123aol.com”)) { 文件。写入(“用户2:+user2.email”); } 编写(窗口['user1']+“
”; document.write(窗口['user2'])
函数createUser(用户名、电子邮件)
{
如果(email.match(/^[a-z0-9!#$%&'*+\/=?^ `{124}-]+(?:\.[a-z0-9!#$%&'*+\/=?^ `{124}-]+)*([a-z0-9](?:[a-z0-9-]*[a-z0-9])+[a-z0
{
窗口[用户名]=新用户(电子邮件);
返回true;
}
其他的
{
返回null;
}
}
功能用户(电子邮件)
{
this.email=电子邮件;
}
如果(createUser(“user1”)bob123@aol.com"))
{
文件。写入(“用户1:+user1.email+”
); } if(createUser(“user2”,“bob123aol.com”)) { 文件。写入(“用户2:+user2.email”); } 编写(窗口['user1']+“
”; document.write(窗口['user2'])
函数createUser(用户名、电子邮件)
{
如果(email.match(/^[a-z0-9!#$%&'*+\/=?^ `{124}-]+(?:\.[a-z0-9!#$%&'*+\/=?^ `{124}-]+)*([a-z0-9](?:[a-z0-9-]*[a-z0-9])+[a-z0
{
窗口[用户名]=新用户(电子邮件);
返回true;
}
其他的
{
返回null;
}
}
功能用户(电子邮件)
{
this.email=电子邮件;
}
如果(createUser(“user1”)bob123@aol.com"))
{
文件。写入(“用户1:+user1.email+”
); } if(createUser(“user2”,“bob123aol.com”)) { 文件。写入(“用户2:+user2.email”); } 编写(窗口['user1']+“
”; document.write(窗口['user2'])
函数createUser(用户名、电子邮件)
{
如果(email.match(/^[a-z0-9!#$%&'*+\/=?^ `{124}-]+(?:\.[a-z0-9!#$%&'*+\/=?^ `{124}-]+)*([a-z0-9](?:[a-z0-9-]*[a-z0-9])+[a-z0
{
窗口[用户名]=新用户(电子邮件);
返回true;
}
其他的
{
返回null;
}
}
功能用户(电子邮件)
{
this.email=电子邮件;
}
如果(createUser(“user1”)bob123@aol.com"))
{
文件。写入(“用户1:+user1.email+”
); } if(createUser(“user2”,“bob123aol.com”)) { 文件。写入(“用户2:+user2.email”); } 编写(窗口['user1']+“
”;
document.write(窗口['user2'])您可以使用
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("bob123@aol.com");
} catch(e) {
    console.log(e);   
}
但在我看来,构造函数应该总是创建一个对象,所以我会这样做:

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("bob123@aol.com");

if(!user1.isValid()){
    user1 = null;
}

您可以使用
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("bob123@aol.com");
} catch(e) {
    console.log(e);   
}
但在我看来,构造函数应该总是创建一个对象,所以我会这样做:

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("bob123@aol.com");

if(!user1.isValid()){
    user1 = null;
}

您可以使用
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("bob123@aol.com");
} catch(e) {
    console.log(e);   
}
但在我看来,构造函数应该总是创建一个对象,所以我会这样做:

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("bob123@aol.com");

if(!user1.isValid()){
    user1 = null;
}

您可以使用
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("bob123@aol.com");
} catch(e) {
    console.log(e);   
}
但在我看来,构造函数应该总是创建一个对象,所以我会这样做:

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("bob123@aol.com");

if(!user1.isValid()){
    user1 = null;
}
如果无效,请删除该对象

不要。更好的方法是,在尝试创建用户之前测试电子邮件地址是否有效

或者什么也不归还

你真的不能。从构造函数中不返回任何内容实际上是不可能的,除非抛出异常

改用额外的出厂功能:

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("bob123@aol.com")
if (user1)
    this.checkValid() // true
如果无效,请删除该对象

不要。更好的方法是,在尝试创建用户之前测试电子邮件地址是否有效

或者什么也不归还

你真的不能。从构造函数中不返回任何内容实际上是不可能的,除非抛出异常

改用额外的出厂功能:

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("bob123@aol.com")
if (user1)
    this.checkValid() // true
如果无效,请删除该对象

不要。更好的方法是,在尝试创建用户之前测试电子邮件地址是否有效

或者什么也不归还

你真的不能。从构造函数中不返回任何内容实际上是不可能的,除非抛出异常

改用额外的出厂功能:

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("bob123@aol.com")
if (user1)
    this.checkValid() // true
如果无效,请删除该对象

不要。更好的方法是,在尝试创建用户之前测试电子邮件地址是否有效

或者什么也不归还

你真的不能。从构造函数中不返回任何内容实际上是不可能的,除非抛出异常

改用额外的出厂功能:

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("bob123@aol.com")
if (user1)
    this.checkValid() // true


假设我正在创建一个新用户var user3=新用户(“bob342@aol.com") ... 检查(!user3.email)console.log(“无效电子邮件”)是否成功的最佳方法是什么?或者是否有更好的方法。是的,简单的
if
就足够了。您甚至可以将其合并到构造函数中。它可能引发错误,或触发另一个显示消息的函数。当前正在生成脚本以执行以下操作:如果电子邮件有效,请设置属性。如果不忽略属性,我尝试将其合并到构造函数中,它不断返回“undefined”,当然,即使电子邮件无效,也会创建一个对象!有没有办法防止这种情况发生呢?假设我正在创建一个新用户var user3=new user(“bob342@aol.com") ... 检查(!user3.email)console.log(“无效电子邮件”)是否成功的最佳方法是什么?或者是否有更好的方法。是的,简单的
if
就足够了。您甚至可以将其合并到构造函数中。它可能引发错误,或触发另一个显示消息的函数。当前正在生成脚本以执行以下操作:如果电子邮件有效,请设置属性。如果没有,请省略该道具