This code completes the challenge successfully, but I’m confused as to how it works.
Whatever is passed in as the firstAndLast
argument when the new object is created can be accessed by the methods. But bob.firstAndLast
returns undefined (since it’s not a property of bob
, and console.log(firstAndLast)
also returns undefined (since it’s not a global-scope variable).
I’ve also tried pasting this code into the Chrome JS console and looking through the prototype chain but there doesn’t seem to be a firstAndLast
variable stored their either.
So where is firstAndLast
stored/how does this work?
**Your code so far**
const Person = function(firstAndLast) {
this.setFirstName = function(first){
firstAndLast = firstAndLast.replace(/\w+(?=\s)/, first);
}
this.setLastName = function(last){
firstAndLast = firstAndLast.replace(/(?<=\s)\w+/, last);
}
this.setFullName = function(newFirstAndLast){
firstAndLast = firstAndLast.replace(/.+/, newFirstAndLast);
}
this.getFirstName = function() {
return firstAndLast.match(/\w+(?=\s)/)[0];
}
this.getLastName = function() {
return firstAndLast.match(/(?<=\s)\w+/)[0];
}
this.getFullName = function() {
return firstAndLast;
};
};
const bob = new Person('Bob Ross');
bob.getFullName();
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Challenge: Make a Person
Link to the challenge:
The variable is captured in the context of the object. I think it’s easier to see here:
Make a Person
Problem Explanation
When I started the program I figured I just had to create the six functions mentioned in the details. However, it was not that simple. Creating them as a function was not the right way, I had to create them in a different way to make them a key.
There is also a tricky part as you need six keys no more or less, so at first I had the variable that store the original name as a key too which was wrong.
As for the usage of array, that is optional, you could als…
For further reading (super interesting stuff), look up closures.
1 Like
const Person = function(firstAndLast) {
this.setFirstName = function(first){
firstAndLast = firstAndLast.replace(/\w+(?=\s)/, first);
}
this.setLastName = function(last){
firstAndLast = firstAndLast.replace(/(?<=\s)\w+/, last);
}
this.setFullName = function(newFirstAndLast){
firstAndLast = firstAndLast.replace(/.+/, newFirstAndLast);
}
this.getFirstName = function() {
return firstAndLast.match(/\w+(?=\s)/)[0];
}
this.getLastName = function() {
return firstAndLast.match(/(?<=\s)\w+/)[0];
}
this.getFullName = function() {
return firstAndLast;
};
};
const bob = new Person('Bob Ross');
console.log(bob.getFullName());
bob.setFullName("Sonu Shivcharan");
//this sets a new name
console.log(bob.getFullName());
//will return "Sonu Shivcharan"
.setFullName(“first name”)
Sets name to the object
system
closed
July 18, 2022, 12:38am
#4
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.