Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6715503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:37:20+00:00 2026-05-26T08:37:20+00:00

I have a several variables which are assigned to the same function . The

  • 0

I have a several variables which are assigned to the same function. The property ‘name’ is “” as this function is anonymous;

There also isn’t a function call involved, and as such no callee.

Is there a way in JS to obtain the variable’s name, through a self-implemented reflection algorithm?

e.g.

var x = function(){}
var myUnknownNamedVar1 = myUnknownNamedVar2 = x;

Background:

for space efficiency, thousands of variable names are assigned to the same ‘static’ function as a lazy-loaded stumps, which get resolved to individual functions upon the first call invocation. (The variable name also contains a namespace).

Alternatives:

My alternative would be the use of objects such as {_name: myUnknownNamedVar1 , _fn: x}

Solutions to this issue would be interesting. A particular issue is discerning variables with different names ,in the script, which point to the same object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:37:21+00:00Added an answer on May 26, 2026 at 8:37 am

    1. You could try parsing the script’s text.

    JSFiddle Example

    var x = function(){}
    var myUnknownNamedVar1 = myUnknownNamedVar2 = x;
    
    var txt  = (document.currentScript && document.currentScript.textContent) ||
               (document.scripts       && document.scripts[1]
                                       && document.scripts[1].textContent);
    var arr  = txt.match(/var\s*(.*)\s*=\s*x\b/);
    arr[1]   = arr[1].replace(/\s+$/,"");
    var vars = arr[1].split(/\s*=\s*/);
    
    // vars: ["myUnknownNamedVar1", "myUnknownNamedVar2"]
    

    2. You can try comparing values

    JSFiddle Example

    var x = function(){}
    var myUnknownNamedVar1 = myUnknownNamedVar2 = x;
    
    var vars = [];
    for (var prop in window){
        if (window[prop] == x)
          vars.push(prop);
    }
    
    // vars: ["x", "myUnknownNamedVar1", "myUnknownNamedVar2"]
    

    Note: Both methods will need refining (eg recursion, better/more match patterns). The second one only looks at global variables, whereas some variables in closures may not be exposed to the global namespace. Also, there are objects of objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function mainFunc which needs to call a several times another function
My User entity class have several member variables (e.g. String firstName) which are persisted
I have an onclick function which performs several tasks. In another JavaScript function I
I have a class with several variables, one of which is a hash (_runs):
i have several classes that have the same function, the are called at different
I have index.php which uploads a file to server and sets several PHP variables
I have a script, which by using several querystring variables provides an image. I
I have a layout which works fine. This layout contains several partials, which display
I have several variables of type char (array), int, and double. Is there a
Say you have several large arrays of variables (which generally are strings), and you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.