(NOTE TO HELPER: this question might not have a solution)
Greetings all,
I was solving a little challenge I gave myself. When I encounter a seemly impossible task. The challenge goes as follow.
TOP LEVEL
Have a function which uses closures to simulate a class. (by class I mean somewhat what you get in C++ or C# when you declare a new class.) The top function will return an inner function which has props and that returns methods to change those props. This means that I cannot access the props directly. (what you get when you declare variables under private in traditional classes)
CODE
const Person = function() { //outer function
const closures = () => { // inner function
let firstName, lastname, age, gender; // "class" properties (props) unknown to Person
return { // returns the methods I can use to update or get the props + functionalities
//setters - can set the properties of closures
setFirstName: (name) => {
firstName = name;
},
setLastName: (surname) => {
lastname = surname;
},
setAge: (newAge) => {
newAge > 0 && newAge < 150 ? age = newAge : console.warn(`Age has not been set, ${newAge} is out of range`);
},
setGender: (newGender) => {
const personExist = (firstName || lastname);
personExist? gender = newGender : console.warn("This person does not exist, you need to define their first or last name . . .");
personExist?checkGender(newGender) ? gender = newGender : console.warn(`Gender has not been set. ${newGender} is not supported at the moment.`) : null;
;
},
//getters - can get the properites of closures
getFirstName: () => firstName,
getLastName: () => lastname,
getAge: ()=> age,
getGender: ()=>gender,
//Functionalities -- things that a Person obj can do
introduction: () => {
let p1 = firstName ? `Grettings, my name is ${firstName}` : lastname ? `Last Name is ${lastname}` : '';
let p2 = firstName ?
lastname ? ` ${lastname}.` : '' :
lastname ? `. You can refer to me as ${lastname}`: '';
let p3 = age? `I am ${age} years-old.` : '';
let p4 = p1 != '' || p2 != '' ? `${lastname?`${p1}${p2} ${p3}`: `${p1}.${p2} ${p3}`}` : "I do not know me . . .";
return `${p4}`
},
speak: function to(Person) { // troublesome part
if(typeof Person == 'object'){
console.log(`> ${Person.introduction()}`);
// console.log(name.caller)
// console.log(name.prototype)
// everything was going well till this part of the challenge, where I am trying to get name.caller so i can access its props
} else {
console.warn(`Cannot talk to ${Person} because ${Person} is not of type person.`)
}
}
}
}
return closures();
}
// this is the entire code, feel free to use it in anyway whatsoever.
PROBLEM
The last part of the challenge where I am struggling on is when I am trying to implement the "speak" function.
-- Expected behavior
A Person instance (not the first ever created) will call speak, passing-in another person instance.
Within speak, the argument will introduce itself using one of the methods declared. Then the caller will introduce itself.
--Troublesome Part
I cannot access "to's" (the function sub-name for speak) caller. (in case your wondering, I need to give it a sub-name because if I try to call "speak" it will yield an error.) The easy solution is to also pass-in the caller as an argument, but I want to be able to access it using the caller method instead.
for example the following code
let rian = Person();
rian.setFirstName("Rian");
rian.setLastName("Arias");
rian.setGender("Male");
rian.setAge(30);
let pepe = Person();
pepe.setFirstName("Pepe");
pepe.setLastName("Lopez");
pepe.setGender("Male");
pepe.speak(dario); // will not work because I haven't been able to get 'caller' to work
Should output the following
> Grettings, my name is Dario Arias. I am 20 years-old. < Grettings, my name is Pepe Lopez.