0

I'm starting to learn OOP in JS and I came across this issue where my alert() will not trigger when validateString returns false. I try personOne.setFirstName(''); but the alert() does not trigger.

//define Name class 
function Name () {
    this.firstName = '';
    this.lastName = '';
    this.middleName = '';
    this.details = {
        eyeColor: '',
        hairColor: ''
    }
};

var validateString = function(p) {
    return typeof p != "undefined" && $.trim(p).length;
};

//begin Name methods
Name.prototype.getFullName = function() {
    return this.firstName + ' ' + this.middleName + ' ' + this.lastName;
};
Name.prototype.setFirstName = function(p) {
    if (validateString) {
        this.firstName = p;
    } else {
        alert('Please enter a valid first name.');
    }
};
Name.prototype.setLastName = function(p) {
    if (validateString) {
        this.lastName = p;
    } else {
        alert('Please enter a valid last name.');
    }
};
Name.prototype.setMiddleName = function(p) {
    if (validateString) {
        this.middleName = p;
    } else {
        alert('Please enter a valid middle name.');
    }
};
Name.prototype.setHairColor = function(p) {
    this.details.hairColor = p;
};
Name.prototype.setEyeColor = function(p) {
    this.details.eyeColor = p;
};

//end Name methods

var personOne = new Name();
personOne.setFirstName('John');
personOne.setLastName('Doe');
personOne.setMiddleName('Barry');
personOne.setEyeColor('Brown');
personOne.setHairColor('Black');
document.write(personOne.getFullName());
document.write(personOne.details.eyeColor);
document.write(personOne.details.hairColor);
5
  • Just a note: you probably should define validateString() as a static Name method; at the moment it is not part of the class even though it is required by many of the Name methods. Commented Jun 29, 2012 at 5:39
  • @DavidJohnWelsh Actually it's fine the way it is when he uses the module pattern :) Commented Jun 29, 2012 at 5:45
  • @Jack What is the "module pattern?" I'm new around here :) Commented Jun 29, 2012 at 5:46
  • @Etcher stackoverflow.com/questions/10691300/… Commented Jun 29, 2012 at 5:48
  • I'm not old around here but not exactly new around here (I'm middle-aged around here...?) and I don't understand, either. I'm sure I'm just being dim (happens a lot :-D), but how is the module pattern relevant here? Commented Jun 29, 2012 at 6:03

2 Answers 2

2

You need to pass an argument to validateString.

if (validateString(p)) {
  //etc
}

EDIT: ... and also, as @potench mentioned, the reason the if statement was evaluated as true is because this:

if (myFunc) {
  //blah
}

means "if myFunc exists as a defined variable". In this case it's the same as if (3) or if (true).

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! Works perfectly now, such a newbie mistake.
The alert does not trigger because validateString is truthy (it's defined as a function so if(validateString) passes since validateString is not null, 0, or undefined.
David you may want to edit your answer to reflect why the alert is not firing (because if(validateString)... is truthy)
0

You don't need validateString in this case. Consider

Name.prototype.setFirstName = function(p) {
  this.firstName = p || alert('please enter a first name');
}

This is called a short circuit boolean. If no parameter is supplied, or the parameter is an empty string, the alert will be triggered and this.firstName will be undefined. If you need to check te length ofp`, use:

Name.prototype.setFirstName = function(p) {
  this.firstName = (p && p.trim().length) || alert('please enter a first name');
}

1 Comment

yes, but if in the future he wants to expand the validation routine (only allow certain characters or whatever), he will need a separate method to deal with it. I think since the OP is specifically trying to learn an OOP way of doing things, compartmentalizing everything is a good idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.