Link to home
Start Free TrialLog in
Avatar of stevefNYC
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-z0-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?
Avatar of stevefNYC
stevefNYC

ASKER

Also, I can split the points up if someone can explain what exactly load is? is that a property of the global window object?
They are called hash arrays like a hashMap collection object in java. Each element in an array is identefied with its key value
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

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
Avatar of basicinstinct
basicinstinct
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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