The solution to the problem at hand is quite straightforward.
It's important to note that both getter
and setter
are properties of type function
, even though they function as a single property where the value could be a function or something else entirely.
The issue arose when you mistakenly redefined the documentation symbol for Person.name
as string
instead of defining it properly as a function
typically should. This error occurred within the context of the setter
which should ideally return void
.
To address this problem, consider using @returns {string}
in place of @type {string}
.
Below is the complete resolution:
class Person
{
/**
* The constructor parameters are specific to each instance.
*
* @param { string } Name The `Name` parameter requires a `string`.
*/
constructor (Name)
{
/**
* Property definitions must occur within the constructor
* to safeguard their private handling conditions.
*/
Reflect.defineProperty
(
this, 'Name',
{
// Configuration lockdown to prevent override
configurable: false,
// Ensuring non-enumerability of property
enumerable: true,
// Getter method returns Name parameter
get: () => Name,
// Setter method updates Name parameter
set: (New) => { Name = String(New).toString() },
}
);
// Initial assignment for parsing purposes
this.Name = Name;
}
// Unsafe section due to potential descriptor replacement
// Getter for Age property, indirectly exposed through `Person.Age`
// Assumes return value will be a `number`
get Age ()
{
return this._Age;
}
// Setter for Age property, indirectly exposed through `Person.Age`
// Expects `New` parameter and converts it to `number`
set Age (New)
{
this._Age = Number(New);
}
}
const Human = new Person;
// Setting name and age values
Human.Name = 'Some Doe';
Human.Age = 33;
// Attempting to manipulate the `_Age` property directly
Human._Age = 'tirty two';
console.log({ ...Human });
I took some shortcuts in certain sections, but there shouldn't be any issues with this code snippet.