2014-10-22 134 views
0

我正在一个项目中,我从另一个对象/函数引用一个变量。然而,我总是返回false。我不确定我是否正确调用它。总是返回false | Javascript |对象

下面是验证功能:

app.validation = function(){ 
    'use strict'; 
    var validCheck = true; 

    if((app.process.user.length < 3) || (app.process.user.length > 50)){ 
     validCheck = false; 
     window.alert("The username is not acceptable. Example: username"); 
    }; 
    if((app.process.name.length < 3) || (app.process.name.length > 50) || (!app.process.name.indexOf(" ") === -1)){ 
     validCheck = false; 
     window.alert("The name is not acceptable. Example: John Smith"); 
    }; 
    if((app.process.email.length < 3) || (app.process.email.length > 100) || (!app.process.email.indexOf("@") === -1)){ 
     validCheck = false; 
     window.alert("The email is not acceptable. Example: [email protected]"); 
    }; 
    if((app.process.passlength < 6) || (app.process.pass.length > 20)){ 
     validCheck = false; 
     window.alert("The password is not acceptable. Example: password"); 
    }; 
    if((app.process.age.length < 1) || (app.process.age.length > 2)){ 
     validCheck = false; 
     window.alert("The age is not acceptable. Example: 22"); 
    }; 
    return validCheck; 
}; 

这是对变量的存储:

app.process = function(){ 
    'use strict'; 
    var user = document.getElementById("user").value; 
    var name = document.getElementById("name").value; 
    var email = document.getElementById("email").value; 
    var pass = document.getElementById("pass").value; 
    var age = document.getElementById("age").value; 

    var test = app.validation(); 
    console.log(test); 
    if(!test){ 
     window.alert("Try Again."); 
    }else{ 
     app.reset(); 
     app.members[app.members.length] = new app.Member(user, name, email, pass, age); 
     app.printMembers(); 
    }; 
}; 

还有更多这个代码但是是到大在这里发表。这是导致问题的两个功能。

回答

1

您不能访问app.process变量,因为它们对函数是私有的。您需要以某种方式将这些值传递给app.validation以验证它们。

我会做的方式是

var data = { 
    user: document.getElementById("user").value, 
    name: document.getElementById("name").value, 
    email: document.getElementById("email").value, 
    pass: document.getElementById("pass").value, 
    age: document.getElementById("age").value 
}; 

var test = app.validation(data); 

而且在验证

app.validation = function(data) { ... 

而且每次更换为app.process.fielddata.field

+0

数据是在流程方法中还是在全局范围内定义? – hudsond7 2014-10-22 00:54:22

+0

其中,总是在:)全局变量是来自地狱。 – 2014-10-22 01:46:37

+0

谢谢你的帮助。 – hudsond7 2014-10-22 11:46:04