What problems are being caused by this specific way of assigning variables?

What is the difference in variable assignment when it is inside an object and the "this" variable refers to that object?

var prop       = this.properties;
var properties = this.properties;

Compared to:

var prop = properties = this.properties;

Switching to the latter resulted in issues. Could it be a problem with referencing?

Answer №1

There is a distinction in the second scenario where you are attempting to assign a value to a variable named properties without first declaring it. The use of var only pertains to prop, not properties, as properties appears on the right side of the assignment operator (=) following prop, making it part of the initialization process for prop. Here is how it breaks down:

var prop = (properties = this.properties);
//         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^−−−− initializer for `prop`

In non-strict mode, this leads to what I refer to as The Horror of Implicit Globals — creating a global variable called properties. When using strict mode, assigning to an undeclared identifier results in the error it should always have been. Hence, all new code should adhere to strict mode.

You can declare multiple variables with var, but you cannot assign them identical values without repeating the source.

You could opt for:

var prop, properties;
prop = properties = this.properties;

If preferred. Alternatively:

var prop = this.properties, properties = prop;

This approach works because initializers within a var (or let or const) involving multiple declarations are executed based on the order in the source code, from left to right. Thus, prop = this.properties executes before properties = prop.


Note: It is advisable to avoid using var in new code and instead opt for let or const. If there is a need to support outdated JavaScript engines, modern code can be transpiled to ES5 standards.

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

Service for Posting in Angular

I am looking to enhance my HTTP POST request by using a service that can access data from my PHP API. One challenge I am facing is figuring out how to incorporate user input data into the services' functionality. Take a look at the following code snip ...

Omitting "a" elements with a designated ancestor from a jQuery scroll operation

I successfully added smooth scrolling to my webpage using the provided script. However, I also have a section with tabs where I do not want the smooth scrolling effect. Here is my jQuery code: $('a[href*="#"]') // Remove links that don&apos ...

Trouble with incrementing in Angular 4's previous and next buttons

Creating a prev and next component to navigate through pages using Angular's router posed a challenge not related to Angular itself, but rather to the logic for incrementing and decrementing. After clicking the next button for the first time, everyth ...

Transform XML2JS by eliminating square brackets around values

Currently, I am utilizing the xml2js node package for parsing an XML feed. Is there a method to avoid having the values enclosed in square brackets? For instance: "reference": ["ABC123"] should appear as "reference": "ABC123" "items": [ { "r ...

Navigating the jQuery UI Accordion: selecting a panel via the top menu to reveal its contents while simultaneously closing any

Being a native French speaker, I ask for your understanding regarding any English errors made. Additionally, while I am comfortable with (x)HTML and CSS, I am a complete beginner in jQuery. My current project involves setting up a jQuery UI Accordion with ...

What is the best way to organize properties within the Class Variance Authority?

Can the following be achieved in CVA? const mathPanelVariants = cva( 'relative mt-[100px] w-full rounded-sm border-l-[3px] px-[24px] py-[16px]', { variants: { variant: { theorem: { box: 'border-l-[#617bff] dark:bg-[#182 ...

Dealing with Error TS2769 in Visual Studio Code when passing props to a custom component in Vue 2 with Typescript

I've encountered an issue with a Vue JS component that involves passing a custom prop. I am utilizing the Vue Options API without utilizing the class component syntax. Whenever I pass ANY prop to my custom component the-header, I receive an error sta ...

Issue encountered when concealing page elements followed by revealing them again

My goal is to conceal an id within a page and then reveal it once all the JavaScript has finished loading on the page. The JavaScript I am using to display the content again is: $(document).ready(function() { $("#HomePage")[0].style.visibility = "visib ...

Is it feasible to activate a Bootstrap Tooltip from a different element?

I am currently developing a system that enables users to add notes over an image they upload. Presently, I have a note div that, upon clicking, opens a text box for editing. When hovering over this div, I utilize Bootstrap Tooltip to preview the contents o ...

Spin the object around the world's central axis

I've encountered several different methods that claim to rotate an object around the world axis, but they all seem to rotate the object around its own axis instead of the world's. Can anyone advise on the correct approach to rotate an object arou ...

Issues with jqGrid's grid grouping feature not functioning as expected

Is there a way to group my data by the name element and hide the grouping column as shown in this example ()? Even though all 3 columns are present on the page, I specifically want to hide the group column (name). Additionally, it seems that the main col ...

Can we safely save a value in session storage directly from the main.js file in Vue?

Throughout the user session managed by Vuex, I have a session storage value set to false that needs to be monitored. Setting up this value directly from the main.js file looks like this: import { createApp } from 'vue'; import App from './Ap ...

Data vanishing in upcoming authentication session in test environment

I have encountered an issue with next auth in my next.js project. During development, the session data is lost if the server refreshes or if I switch to another tab and return to it. This forces me to sign out and then sign back in to restore the session d ...

Comparison of Vue CLI 3 vue.config.js and webpack.config.js in terms of handling plugins

Currently in my Vue CLI 3 setup, I'm encountering an issue with implementing the Terser Webpack Plugin to remove console.log and comments from the code. Despite following the production workflow below: Execute npm run build Run serve -s dist vue.co ...

"Implementing a 2D graphical user interface on a three.js

Struggling to create a 2D graphical user interface on the renderer. The challenge lies in positioning the 2D GUI dynamically based on the width of an element drawn in threejs (the element has a width of X using threejs units, and the menu needs to be posit ...

Guide on confirming the presence of an element on a webpage utilizing Node.js, Mocha, and Selenium

Is there a way to confirm the presence of an element on a webpage now that selenium v3 has removed the "isElementPresent" functionality? Can you provide an example of how to accomplish this without using driver.isElementPresent? For instance, if I need to ...

What exactly occurs when a "variable is declared but its value is never read" situation arises?

I encountered the same warning multiple times while implementing this particular pattern. function test() { let value: number = 0 // The warning occurs at this line: value is declared but its value is never read value = 2 return false } My curi ...

"Enhance your website's user experience with a sleek multiple selection

I'm attempting to develop a reset button for jquery-select2 multiple select, but for some reason, my solution isn't working as expected. Here is the HTML code: <select id="workload-selector" class="js-source-states-2" multiple="multiple" sty ...

Eliminate the CSS triggered by a mouse click

Having some difficulty toggling the switch to change the background color. Struggling with removing the applied CSS and getting it to toggle between two different colored backgrounds. Code Sample: <!Doctype html> <html lang="en"> <head> ...

"Return to previous view with the zoom back feature in CanvasJS

How can I implement a zoom back button in CanvasJS using jQuery or normal JavaScript? I've been attempting to place it near the ones in the top right corner, but something seems to be amiss. Alternatively, is it feasible to enable zooming in and out ...