stevefNYC
asked on
Question about JavaScript errMsg{} object
Hi all,
Have a question about an authors intention here when returning an object's value back to the calling function:
var errMsg = {
// Checks for when a specified field is required
required: {
msg: "This field is required.",
test: function(obj,load) {
// Make sure that something was not entered and that this
// isn't on page load (showing 'field required' messages
// would be annoying on page load)
return obj.value || load || obj.value == obj.defaultValue;
}
},
// Makes sure that the field s a valid email address
email: {
msg: "Not a valid email address.",
test: function(obj) {
// Make sure that something was entered and that it looks like
// an email address
return !obj.value ||
/^[a-z0-9_+.-]+\@([a-z0-9- ]+\.)+[a-z 0-9]{2,4}$ /i.test( obj.value );
}
},
</snip>
That's just part of the code, but as you can see, he's consistently using !obj.value... why would you want to return it in this fashion?
Have a question about an authors intention here when returning an object's value back to the calling function:
var errMsg = {
// Checks for when a specified field is required
required: {
msg: "This field is required.",
test: function(obj,load) {
// Make sure that something was not entered and that this
// isn't on page load (showing 'field required' messages
// would be annoying on page load)
return obj.value || load || obj.value == obj.defaultValue;
}
},
// Makes sure that the field s a valid email address
email: {
msg: "Not a valid email address.",
test: function(obj) {
// Make sure that something was entered and that it looks like
// an email address
return !obj.value ||
/^[a-z0-9_+.-]+\@([a-z0-9-
}
},
</snip>
That's just part of the code, but as you can see, he's consistently using !obj.value... why would you want to return it in this fashion?
They are called hash arrays like a hashMap collection object in java. Each element in an array is identefied with its key value
ASKER
gops1: what does that have to do with how the author is returning !obj.value or what load is?
this is a better way of return. Instead of writing two lines of code it is written in one single line
this statement --> obj.value || load || obj.value == obj.defaultValue will return you either true or false, hence it is equivalent to
return false or return true
return false or return true
ASKER
Unfortunately I'm still not completely aware of what it's trying to evaluate or why it would return it in this fashion, so I'm going to leave this question up for others to give it a shot.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>return !obj.value
>>if obj.value is empty or null or undefined this statement will return true
I should have added, it will obviously return true if it is false also, as gops said
>>if obj.value is empty or null or undefined this statement will return true
I should have added, it will obviously return true if it is false also, as gops said
ASKER