Skip to content

Instantly share code, notes, and snippets.

@kena0ki
Last active March 28, 2020 02:17
Show Gist options
  • Save kena0ki/fc51bb45a509687d2afdbcdc028c9e9a to your computer and use it in GitHub Desktop.
Save kena0ki/fc51bb45a509687d2afdbcdc028c9e9a to your computer and use it in GitHub Desktop.
I expected fnc prints MyClass, at least the last one, but actually it printed undefined.
'use strict'
function MyClass() {
function fnc() {
console.log(this, 'in fnc');
}
const arrow = () => {
console.log(this, 'in arrow');
}
const bind = function() {
console.log(this, 'in bind');
}.bind(this)
this.method = () => {
fnc(); // undefined
arrow(); // MyClass
bind(); // MyClass
console.log(this, ' in method.'); // MyClass
}
fnc(); // undefined
}
const obj = new MyClass();
obj.method();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment