Solución
solution.tsTypeScript
export function getThisType(
context: 'method' | 'arrow' | 'unbound'
): string {
switch (context) {
case 'method': {
const obj = {
name: 'Pedro',
test() {
return this;
}
};
return obj.test() === obj
? 'this es el objeto'
: '';
}
case 'arrow': {
class Person {
name = 'Pedro';
test = () => {
return this;
};
}
const p = new Person();
const extracted = p.test;
return extracted() === p
? 'this es la instancia'
: '';
}
case 'unbound': {
class Person {
name = 'Pedro';
test() {
return this;
}
}
const p = new Person();
const extracted = p.test;
try {
return extracted() === undefined
? 'this es undefined'
: '';
} catch {
return 'this es undefined';
}
}
}
}0respuestas