Working on fabric.js, I encountered this code snippet:
canvas.on("object:modified", (opt) => {
console.log(opt.action); //="scale"
handleScaled(opt);
});
In the handleScaled
function, the opt.action
is showing as undefined:
function handleScaled(opt) {
console.log(opt.action) //=undefined
console.log(opt) //displays the whole opt object but there is no "action" in it
//further handleScaled code
}
console.log(opt.action)
displays "scale" correctly before the function call, but becomes undefined within handleScaled
.
Why does opt.action
only exist outside of the function scope and not inside after calling it?
The corrected code ensures handleScale
is only called when the action is "scale":
canvas.on("object:modified", (opt) => {
if (opt.action === "scale") {
handleScaled(opt);
}
});