Another useful thing to note here..
When trying to obtain 'this' within an arrow function, 'this' could be referred to by the variable name of the object:
// let lib = require('custom-library');constobj={// note: the 'obj' can be used to access the 'this' you were looking for.method:()=>console.log(obj),// 'obj' represents itself as 'this'another:function(){console.log(obj);// even better to use 'obj' in a function method scope too, because 'this' would walk up the proto chain and give you the global.},getMethod:()=>{return()=>{console.log(obj);}}}// Let's try itobj.method();// { method: [Function: method], another: [Function: another], getMethod: [Function: getMethod] }obj.another();// { method: [Function: method], another: [Function: another], getMethod: [Function: getMethod] }// Let's try the other problem and see if this solves it as well.letanotherObj=obj;anotherObj.method();// { method: [Function: method], another: [Function: another], getMethod: [Function: getMethod] }anotherObj.another();// { method: [Function: method], another: [Function: another], getMethod: [Function: getMethod] }letanother=obj.another;another();// { method: [Function: method], another: [Function: another], getMethod: [Function: getMethod] }letmethod=anotherObj.getMethod();method();// { method: [Function: method], another: [Function: another], getMethod: [Function: getMethod] }// Another test caseconstfunc=()=>{console.log(func);}func();// [Function: func]
With that being said.. would it be better to just forget about the use of this and just use the objects name as a replacement?
disclaimer: Though, I haven't found any note-able pitfalls to this approach yet. I'd encourage you all to do your own research before using this method.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Another useful thing to note here..
When trying to obtain 'this' within an arrow function, 'this' could be referred to by the variable name of the object:
With that being said.. would it be better to just forget about the use of this and just use the objects name as a replacement?
disclaimer: Though, I haven't found any note-able pitfalls to this approach yet. I'd encourage you all to do your own research before using this method.