Object keys exist globally, independent of function scopes

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);
  }
});

Answer №1

When examining your code, a distinct behavior is observed when displaying the value of opt.action before and after calling the handleScaled function. Initially, it correctly shows as "scale" due to accessing the action property of the opt object within the callback function triggered by the canvas.on event handler. However, logging opt.action within the handleScaled function results in displaying undefined. This discrepancy arises because the opt object passed to handleScaled is a different instance lacking the action property.

In order to ensure the accessibility of opt.action within the handleScaled function, you can make adjustments to your code as shown below:

canvas.on("object:modified", (opt) => {
  console.log(opt.action); //="scale"
  handleScaled(opt);
});

function handleScaled(opt) {
  console.log(opt.action); // "scale" (expected result)
  // additional handleScaled logic
}

By directly passing the opt object to the handleScaled function, you maintain access to its properties within the function's scope. It is crucial to ensure that the opt object provided to handleScaled includes the anticipated action property.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Rotating a cone in three.js using two points and a specified radius

Using the coordinates and radius provided in an example XML file, I am trying to create a cone. Is there a way to rotate the cone if I have the top vertex's coordinates and the center of the base? Here is an image for reference: Image ...

The chosen option in the q-select is extending beyond the boundaries of the input field

Here's the code snippet I used for the q-select element: <q-select square outlined fill-input standout="bg-grey-3 text-white" v-model="unit_selection" :options="units&qu ...

Leverage JavaScript or CSS to create a paper button without the ink effect

Currently, I am utilizing JavaScript to dynamically create paper-buttons in the following manner: function createButtons(dieDefaults) { for(var i = 0; i < dieDefaults.length; i++) { var btn = document.createElement("paper-button"); var txt = d ...

Extracting values from PHP using AJAX

Whenever a user clicks on the download button, a zip file is successfully created on the server with the necessary files. However, instead of getting an alert with the location of the zip variable ($zip) from PHP as expected, it shows [object Object]. The ...

An Observable object generates output even if there is no listener to receive it

In my current Angular2 project, I am utilizing observables. It's my understanding that each Observable instance includes an observer on a one-to-one basis. By using observer.next(value) to emit something, we can then retrieve that value using observab ...

A useful tip for emphasizing tags that have attributes with endings

Here is the HTML list I'm working with: <li class=​"info" data-info=​"" data-title=​"info 1" data-info-id=​"222643">…</li> <li class=​"info" data-info=​"" data-title=​"info 2" data-info-id=​"217145">…</li> ...

Execute the following onclick event when the button is enabled

I am trying to display a popup when the "add to cart" button is clicked (but only when the button is active!). I thought about using a div that wraps the button and changes the class from "disabled" to "enabled". I have tried using jQuery, but it could als ...

What is preventing me from using Selenium/Javascript to control Firefox on Ubuntu 22.04, when I am able to do so with Python?

My python script effectively controls Firefox using Selenium: from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Firefox() driver.get("https://dev.to") driver.find_element(By.CLASS_NAME, "crayons ...

What is the best way to display a 404 error page for a nonexistent child page on a WordPress site?

After building our site with WordPress, I noticed that non-existent child page routes are not being redirected. For instance, let's say we have a page called about-us with the URL http://example.com/about-us. If I try to access a non-existing child p ...

What is the best way to maintain the value of variables across different HTML files?

Imagine having a scenario where a page is connected to a .js file. This file contains code that assigns a value to a variable like this: var foo; function bar() { foo = //value generated through user input } bar(); Now the goal is to move t ...

Check out the CSS media query for screens with a maximum width on larger devices

Is there a way to preview a specific div and see how it appears on mobile devices? The HTML code for the div is obtained from an external source, like this: var external_html='<div>Responsive div</div>' + '<style>@media ...

Tips for integrating Twitter sharing functionality in React JS

I am looking for a way to enable users to easily share images from my website on Twitter. Although I tried using the react-share module, it does not provide an option to directly share images. This is the snippet of code I currently have: import { Sh ...

Overriding JSON variables globally

In my project, I needed to utilize json2.js since the JSON object in the browser (IE8) was not capable of parsing strings into JSON. After reviewing json2.js, I have a question regarding the variable declaration within it. The json2.js file declares a gl ...

Harness the power of JavaScript to generate a dynamic overlay with a see-through image that can be expanded

Within different sections of my website, we display banner ads that are loaded in real-time from third-party sources and come in various sizes. I'm interested in adding a transparent overlay image to each ad which would allow me to trigger a click ev ...

Component for liking items built with VueJs and Laravel

I'm currently delving into the world of VueJS, and for my personal project, I'm pairing it with Laravel 5.7. However, I'm facing a bit of a challenge when it comes to implementing a simple feature - a "like" button/icon. Here's the sce ...

Chai can't verify the HTML component

During my testing of an Angular project, I initially used Jasmine to check if a specific attribute of an HTML element exists. The following code snippet worked successfully for me: expect(element(by.css("*[id='ref_button']")).getAttribute(&ap ...

adding a JavaScript module to a handlebars template

I have a few different files that I'm working with: In the server.js file, I have the following code: app.get('/main/:id', function(req, res) { res.render('main', { products: products }); }) Within the main.hbs file, I have ...

Emulate clicking a radio button (using PHP and JS)

For the past week, I've been struggling to solve this issue with no luck. I admit that I am new to this area, so I ask for your patience. My current problem involves using TastyIgniter, an online food ordering system. In order to add items to the car ...

Executing a series of post requests within a loop and handling nested promises

Hello, I'm looking for some assistance! I'm attempting to send a post request for each item in an array. The issue I'm facing is that I need to aggregate the responses from each post request and then pass the count array back to the callin ...

Adding an existing element to the DOM using Javascript/jQuery

I am facing an issue in my DOM where I have an element with two children, a div block and a button. My goal is to add a new copy of the same div block to the parentNode whenever the button is clicked. Currently, I am using the following code in the button ...