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

  • SEARCH
  • Home
  • 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 4620490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:34:03+00:00 2026-05-22T02:34:03+00:00

I’m making a google chrome extension and trying to get reference of a local

  • 0

I’m making a google chrome extension and trying to get reference of a local variable within a closure scope.

// The script model of the target website
// I can't change any code of these
function Player(playerName){
    this.name = playerName;
    this.score = 0;
}

function Match(playerRed,playerBlue){
    var player_red = new Player(playerRed);
    var player_blue = new Player(playerBlue);
}

var tennis = new Match("Mike","John")

so what I’m trying to do in my content script is to inject a function into prototype of Match
just to get the variable player_red and player_blue:

function Match(playerRed,playerBlue){
    var player_red = new Player(playerRed);
    var player_blue = new Player(playerBlue);

    //hoping to add this into Match.prototype
    this.showMatchInfo = function(){
            alert(player_red.name + " vs " + player_blue.name);
    }
}

but this will not work because player_red and player_blue isn’t defined under this.

I found this question through search. The solution is to “wrap the constructor in a new constructor and then set the prototypes equal”. Unfortunately this doesn’t work for me as I have no access to the original script of the website and probably because:

  • even by create new myMatch, the new myMatch doesn’t not inherit the player_red and player_blue variable from their original Match instance.
  • Are there any possible workarounds? Thanks.
  • 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-22T02:34:03+00:00Added an answer on May 22, 2026 at 2:34 am

    Notes on “partial solution”:

    Please note that the code snippets posted below only show “some alternatives which may or may not provide enough to get by”. This is because they don’t capture the values (Player objects) within the constructor, but only wrap the values going inside.

    A “full solution” might also wrap the Player constructor and use a property or other mechanism to “remember” the objects created for different input values; alternatively, it could remember object creation order. This could then be used to wrap Match and then extract the created Players from the shared store after the Match constructor had run — those details, however, are left as an exercise. The Player wrapping code can utilize the code presented below (assuming Player is a global/accessible property).


    The exact request is not possible given the above context.

    Variables (real variables, not properties) can only be accessed from the scope they are declared in or a nested scope as they are resolved through scope chains. This also includes usage of eval. While this may seem like a limitation, it also ensures that scope chains (and their variables) can’t be externally mucked with unless exposed.

    However, consider this fun approach, which utilizes the fact that an explicit object can be returned from a Constructor:

    var oldMatch = Match
    // note this form, else above would be pre-clobbered
    Match = function Match (playerRed, playerBlue) {
        var m = new oldMatch(playerRed, playerBlue)
        // either "inject" method here, or save in object for later
        m.myPlayerRed = playerRed
        m.myPlayerBlue = playerBlue
        return m
    }
    

    Of course, this will break things like new Match(...) instanceof Match.

    Happy coding.


    Update:

    Here is a modification of the above to work with the “wrap the constructor in a new constructor and then set the prototypes equal” method as discussed in the link in the post. The trick is “stealing” the global properties name. I have also altered the code to keep oldMatch “private” to avoid pollution.

    // note this form, else Match property would be pre-clobbered
    Match = (function (oldMatch) {
        function Match (playerRed, playerBlue) {
            oldMatch.call(this, playerRed, playerBlue);
            // either "inject" method here, or save in object for later
            this.myPlayerRed = playerRed
            this.myPlayerBlue = playerBlue
        }
        Match.prototype = oldMatch.prototype
        return Match
    })(Match)
    

    Unlike the first code snippet, this should work with new Match(...) instanceof Match, but it may still break depending upon particular assumptions made within the Match object methods.


    Example of how to invert (“extract”) data from Player constructor:

    // original -- remember this method will only work
    // if Player is used as a property (and not itself a closure'd variable)
    function Player (name) {
        this.name = name
    }
    
    Player = (function (oldPlayer) {
        function Player (name) {
            oldPlayer.call(this, name)
            var fn = arguments.callee
            fn.recent = fn.recent || []
            fn.recent.push([name, this])         
        }
        Player.prototype = oldPlayer.prototype
        return Player
    })(Player)
    
    var p1 = new Player("fred");
    var p2 = new Player("barney");
    
    alert("instanceof check? " + p1 instanceof Player)
    alert("name check? " + ("barney" == p2.name))
    
    alert(Player.recent.join(","))
    Player.recent = [] // reset
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.