HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Build a Person class, delegate don't duplicate

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
personduplicatedelegateclassbuilddon

Problem

I am currently learning javascript and I attempted to make a Person class (note - this was part of a practice exercise, so some of the design decisions may have been due to the requirements).

Specifically, I am looking for feedback on:

-
Usage of prototypes (i.e. from my understanding, prototype should be used when the particular behaviour/property is shared with all objects. This means there is only 1 instance of the function (e.g.) getFullName() which is shared across all objects, whereas the properties fullName, firstName and lastName which are declared directly in the Person function, thus a new unique copy will be created in every instance.

-
Usage of undefined (i.e. exercise required us to initially have obj.firstName to be equal to undefined. But does it make more sense to have these properties equal to undefined, or to have it equal to null, or to equal an empty string (like "").

-
Usage of this (am I generally correct in the usage of this? Particularly when prototypes are used)

```
var Person = function(firstAndLast) {
this.fullName = firstAndLast;
this.firstName = undefined;
this.lastName = undefined;
};

Person.prototype.setFullName = function(firstAndLast){
this.fullName = firstAndLast;
};
Person.prototype.setFirstName = function(first){
this.firstName = first;
};
Person.prototype.setLastName = function(last){
this.lastName = last;
};
Person.prototype.getFullName = function(){
return this.fullName;
};
Person.prototype.getFirstName = function(){
//exercise required a default firstName to be set, only if this function was called
if(this.firstName === undefined){
this.firstName = this.defaultFirst();
}
return this.firstName;
};
Person.prototype.getLastName = function(){
//exercise required a default lastName to be set, only if this function was called
if(this.lastName === undefined){
this.lastName = this.defaultLast();
}
return this.lastName;
};

Solution

-
Just so you know, undefined in JS is mutable. It's value can be changed. You could resort to using void 0 instead of undefined.

-
In JS, assignment operations are as good as values, equivalent to the value assigned. Also, the || acts like a "default" operation, where if the left-side value is falsy (ie, blank string, undefined, null, false, 0), the right-side value is used. And so, getFirstName (and similarly, getLastName) can be simplified into:

return this.firstName = this.firstName || this.defaultFirst();


-
For defaultFirst and defaultLast, essentially you're splitting by space and getting the first and last contiguous word, respectively. And so, it can be simplified using split and shift or pop:

// For first name:
return this.fullName.split(' ').shift();

// For last name:
return this.fullName.split(' ').pop();

Code Snippets

return this.firstName = this.firstName || this.defaultFirst();
// For first name:
return this.fullName.split(' ').shift();

// For last name:
return this.fullName.split(' ').pop();

Context

StackExchange Code Review Q#96989, answer score: 2

Revisions (0)

No revisions yet.