Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

A method of accessing 'this' inside of an arrow function

Notifications You must be signed in to change notification settings

hrt/ArrowFunctionContextJS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

It can be useful to access this inside of functions where the context is defined by bind / apply calls. Arrow functions do not work like normal functions. this - is unbindable.

However, we can access the binded this using some magic:

var fn = _=>{
	Error.prepareStackTrace = function(error, structuredStackTrace) {
		return structuredStackTrace[0].getThis();
	}
	try {
		null.test;
	} catch(e) {
		_this = e.stack;
	}
	delete Error.prepareStackTrace;
	console.log('_this: ' + _this)
};
fn.apply("you_can't_access_me", []);```

About

A method of accessing 'this' inside of an arrow function

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published