You cannot put expressions in an object definition. If you want the code to execute after instantiating the object, you should use:
function Validator() {
if(this.formIsValid) {
alert('Form is valid!');
}
else {
alert('Form is invalid...');
}
}
Validator.prototype = {
formIsValid: true,
enforceTextFieldMinLength: function(field, minLength) {
if (!field.value || field.value.length < minLength) {
this.formIsValid = false;
}
},
enforceLabelHasText: function(label) {
if (!label.text) {
this.formIsValid = false;
}
}
}
var a = new Validator();
This is a fictitious solution; you need to add function arguments Validator()to initialize formIsValidother values as well. I suggest you read the description of MDC on prototypes .
EDIT . If you went with a solution prototype, you need to call val.enforceLabelHasText(FirstName)by making a valglobal variable (either omitting varor using var window.val = new Validator()).