Skip to content

Instantly share code, notes, and snippets.

@GeoffreyHervet
Created January 25, 2022 11:05
Show Gist options
  • Save GeoffreyHervet/1b9883eb8d1c4db8007487d06d4535d9 to your computer and use it in GitHub Desktop.
Save GeoffreyHervet/1b9883eb8d1c4db8007487d06d4535d9 to your computer and use it in GitHub Desktop.
class MyClass {
constructor(private name: string){}
func1 = () => { return this.name; }
func2() {
return this.name;
}
}
const object = new MyClass('MyName');
console.log(object.func1());
console.log(object.func2());
console.log('~'.repeat(10));
[ object.func1, object.func2 ].forEach(funcToCall => {
console.log(funcToCall());
})
@GeoffreyHervet
Copy link
Author

Output
MyName
MyName
~~~~~~~~~~
MyName
/Users/geoff/ok.ts:6
		return this.name;
              ^
TypeError: Cannot read properties of undefined (reading 'name')
    at func2 (/Users/geoff/ok.ts:6:15)
    at /Users/geoff/ok.ts:16:14
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/Users/geoff/ok.ts:15:32)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Module.m._compile (/Users/geoff/node_modules/ts-node/src/index.ts:1371:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/geoff/node_modules/ts-node/src/index.ts:1374:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment