I’m still getting used to objects and constructors, so this has me a bit confused.
My solution is correct, but I don’t totally understand at the concept level why firstAndLast is accessible after the object is created.
More specifically, how am I able to access the the firstAndLast property for the getter and setters after the object is created when there is no firstAndLast property defined on the object?
var Person = function(firstAndLast) {
// Complete the method below and implement the others similarly
this.getFullName = function() {
return firstAndLast;
};
this.getFirstName = function() {
let spaceIndex = firstAndLast.indexOf(" ");
return firstAndLast.substring(0,spaceIndex);
};
this.getLastName = function() {
let spaceIndex = firstAndLast.indexOf(" ");
return firstAndLast.substring(spaceIndex+1);
};
this.setFirstName = function(first) {
firstAndLast = firstAndLast.replace(/\w*/,first);
};
this.setLastName = function(last) {
firstAndLast = firstAndLast.replace(/(\s)(\w*)/," " + last);
};
this.setFullName = function(newName) {
firstAndLast = firstAndLast.replace(/.*/,newName);
};
};
var bob = new Person('Bob Ross');
bob.getFullName();