alyssaxuu / flowy

The minimal javascript library to create flowcharts ✨
MIT License
11.48k stars 998 forks source link

"this" property is undefined #96

Closed ebihimself closed 4 years ago

ebihimself commented 4 years ago

I tried to implement the object orient of the main.js as below:

class Hello{
        templateBlock2
    constructor(){
             flowy($("canvas"), this.drag, this.release, this.snap)
        }
    ...

    drag = (block) => {
        block.classList.add("blockdisabled");
        this.tempblock2 = block;
    } 
}

but I receive the error because this is not in the callback scope:

cannot set the property this of undefined

Is there any way that I can pass "this" property to the callbacks?

Kannndev commented 4 years ago

try replacing arrow functions with normal functions that should help.

Kannndev commented 4 years ago

drag(block) { block.classList.add("blockdisabled"); this.tempblock2 = block; }

ebihimself commented 4 years ago

I don't think that it has something to do with the arrow function as I think the "this" keyword is not defined in the flowy.min.js as I pass the object of the drag method to the flowy. and the method is trying to find the property there.

Update The change you mentioned didn't fix.

drag(block) { block.classList.add("blockdisabled"); this.tempblock2 = block; }