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
You will need a closure to hold data that is not a property.
Relevant Links
Closures
Details of the Object Model
Hints
Hint 1
The challenge checks for how many keys you used. Any additional variables you declare have to be in the closure of the object and not properties.
Solutions
Solution 1 (Click to Show/Hide) const Person = function(first, last) {
let firstName = first;
let lastName = last;
this.getFirstName = function() {
return firstName;
…
For further reading (super interesting stuff), look up closures.
2 Likes
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.