#Access class function from within function

3 messages · Page 1 of 1 (latest)

pallid bridge
#

Hello,
i have something like
class Test{ children = [] toggleNode = function () { this.children.forEach(function (childNode) { this.showNode(); }); } showNode = function () {} }

I can't access showNode though - why is that?

#

(this.showNode is not a function)

lime crescent
#

In JS, the this keyword is not the same than in c++/java. It can get messed fast.
I would suggest writing this

const {showNode} = this;
this.children.foreach((child)=>{
 shownode();
})

if not, you may want to play with .bind() but that's not my cup of tea