What is the best way to incorporate this locale variable into the primary script?

Is it possible to access the locale variable from the main script and convert it into a global variable? The current method I tried does not seem to be working. Any advice or guidance is appreciated.

    //Converting a local variable to a global variable

    (function (exampleCode) {
    "use strict";

    var wert = 0.5;
    var name = 'wert';

    Object.assign( exampleCode, {
  
      getValue: function() {
    
        return name;
      
      }
    
    } );

    } )( window.exampleCode = window.exampleCode || {} );


    /////////////////////////////////////////////////////////////////////

     //How can we retrieve 'var wert = 0.5' from the Test Script in the main script?

        var update = function() {
     requestAnimationFrame(update);    
    //var value = wert;     //0.5 
    console.log( exampleCode.getValue() );
    mesh.morphTargetInfluences[ 40 ] = params.influence1 = value; 
};

update();

Answer №1

To retrieve a variable from the outer scope of the getValue function, you can store your data in an object and then access it using myVars[propName]; within the getValue(propName) function by specifying the property name. This way, the value (0.5) will be accessed through the closure.

(function (exampleCode) {
    "use strict";

    let myVars = {
        wert: 0.5
    };

    Object.assign( exampleCode, {
        getValue: propName => myVars[propName]
    } );

    } )( window.exampleCode = window.exampleCode || {} );
    
    let update = function() {    
        let value = exampleCode.getValue('wert');     //0.5 
    console.log( value );
    //mesh.morphTargetInfluences[ 40 ] = params.influence1 = value; 
};

update();

Another approach is to define the wert property within exampleCode and retrieve its value using this:

(function (exampleCode) {
    "use strict";

     Object.assign( exampleCode, {
         getValue: function(propName) { return this[propName]; },
         wert: 0.5
     } );
    
 } )( window.exampleCode = window.exampleCode || {} );
        
 let update = function() {    
     let value = exampleCode.getValue('wert');  
     console.log( value );
 };
    
 update();

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

Codepen for showcasing your Javascript and Jquery skills

I'm having a bit of trouble with the .getjson function. I've been working on retrieving data from a Wikipedia API using .getjson, and while everything seems to be working fine, there's a peculiar issue when I try to find the length of an arr ...

Res.redirect() function does not redirect the browser URL as expected when triggered by a request made through a frontend fetch() call

Encountering a new issue that is challenging me. In the backend, there is an API route that redirects the browser URL to /signin using res.redirect('/signin'). Upon this redirection, React Router triggers the rendering of a 'log back in&apos ...

Is it possible to retrieve data from Local Storage using user_id and SessionId, and if so, how can it be done?

I have some data in an interactive menu created with iSpring, which includes a feature for local storage to save the last viewed page. I also have a system for logging and need to associate this local storage with user_id or sessionid. I found some informa ...

Modifying CSS using jQuery can be achieved with the .parent().addClass method, however, using just .addClass

When I first started working on my validation using jquery/javascript, it was not very effective. However, with the implementation of a new css class and updates to my jquery validation, things have greatly improved. I've been focusing on enhancing u ...

I am having trouble specifying a class within an SVG using D3.js

I have been facing a strange issue with my script. When I try to execute the following code: var key="#US" d3.select(key).style("fill", "red"); it seems like it's not working for me. Interestingly, when I run the same co ...

Error: The React component throws a TypeError because it is unable to read the property 'map' from an undefined source

I encountered the following error TypeError: Cannot read property 'map' of undefined at ListItemFactory.ts:84:57 at The specific line where the error occurs is: return announcementitems=json.value.map((v,i)=>( To provide mor ...

How to update JSON file on server using JavaScript

I am encountering an issue that has been discussed quite frequently. Despite trying various solutions, none have proven effective so far. The problem lies in my JavaScript file where I have some data that I need to append to an existing .json file on my s ...

What is the best way to rearrange elements within an object when there is no predetermined starting order?

Someone assisted me in refining this code snippet import React, { useEffect, useState } from "react"; import _ from "lodash"; // const SeleccionClientes = ""; const items = [ { client: "Microsoft", idClient: 0, idProjectType: 1, project ...

Ways to evaluate the amount of traffic on a webpage?

Recently, I encountered an issue while creating a page to showcase blog posts. Each post had the typical social media share buttons like "Facebook like," "tweet this post," and "+1." Additionally, there were some extra JavaScript functions added for variou ...

How can Conditional Rendering be applied within List Rendering (v-for) in Vue JS?

In my current project, I am utilizing List Rendering (v-for) to display information about various books stored in an array. One challenge I'm facing is implementing conditional rendering within this loop to dynamically generate li elements that repre ...

Unable to access local JSON file data in React.js

I have been struggling to retrieve data from a JSON file located within my ReactJS project directory. Even attempting to access the package.json file within the ReactJS folder resulted in errors. How can I successfully extract data from a local JSON file ...

Find a partial match in the regular expression

I am currently working on filtering a list of names using regular expressions (regex). The data is stored in the following format: [ { "firstName": "Jhon", "lastName": "Doe", }, ... ] Users have the option t ...

How to Transfer a jQuery Variable to a PHP script within a Modal using AJAX

I am currently facing an issue with retrieving the value of a variable and displaying it in a modal that is supposed to open. I have attempted to use the POST method but so far, no changes have occurred. Both sets of code are located within index.php. Her ...

Tips for concealing the body description on the homepage of a blogger website

I want to conceal this description This is my blog and I am facing an issue where I need to hide the description on the home page. I have tried using color:white which worked initially, but when I moved the description to the top or left, the black backgro ...

What is preventing the redefinition of the display property for this element?

Despite setting the display property of the div element with class layer to flex, it is not being displayed on the DOM. $("#elem").click(function(e) { if (e.target.classList.contains("myClass")) { $("*").hide(); ...

Tips for verifying the inclusion of the + symbol in the country code in Angular 7

My form includes a text box where users can input a country code preceded by a + sign. If a user enters only numbers, we need to restrict that action and prompt them to enter the country code with a + sign, for example: +91, +230, ... I've been tryin ...

Enabling individuals to transfer their content to Amazon S3

I have set up an S3 bucket named BUCKET in region BUCKET_REGION. I want to enable users of my web and mobile apps to upload image files to this bucket, with specific restrictions based on Content-Type and Content-Length (specifically, only allowing jpegs u ...

When sorting in AngularJS using the orderBy filter, remember that string values should come before numeric values: for example, sort as follows (n/a, 0,

While running an AngularJS filter sorting on a table, I encountered an issue where if the value is 'n/a' (which is a numeric string), the sorting is incorrect. I expected the order to be n/a, 0, 1, 2, 5, 100 since strings should be considered l ...

working with html data in a bootstrap modal using javascript

Utilizing an id, I pass data to a bootstrap modal and link that id with stored data. $("#fruit").html($(this).data("fruit")); In addition, I am appending data to the bootstrap modal $('#mymodal').find('.modal-body').append('< ...

Is your WordPress one-page scroll JQuery script failing to function properly?

Currently attempting to develop a single page scroll feature. Within WordPress, my navigation contains anchor tags structured as follows: <a href="#contact">Contact</a> <a href="#about">About</a> The corresponding divs for the li ...