Getting a variable reference, or accessing call objects?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Marshall

    Getting a variable reference, or accessing call objects?

    Does anyone know....

    In JavaScript, is there any way to get a reference to a string variable
    (not an object), like Perl's "\" operator? I want to be able to compare
    two such references and know if they point to the same string variable.

    Alternately, is there a way to access the call object in a function, i.e.
    the object that function-local variables are a part of? How about the
    chain of call objects, e.g. if function a() calls function b(), then from
    within b() is it possible to get function a()'s call object? I don't
    think Function.caller does this, and that's deprecated anyway.

    The problem is this: I have an app that reads and modifies JS code in Web
    pages. In the following code I need to know whether the "src=bar" line
    operates on the global window.src, or on the local "src" variable in f(),
    when the "var src" line may or may not be there. Is there any way to do
    this, short of parsing the function and keeping track of all "var"
    declarations?

    src= foo ;
    function f() {
    src= bar ;
  • Douglas Crockford

    #2
    Re: Getting a variable reference, or accessing call objects?

    > In JavaScript, is there any way to get a reference to a string variable[color=blue]
    > (not an object), like Perl's "\" operator? I want to be able to compare
    > two such references and know if they point to the same string variable.[/color]

    In JavaScript, all strings are immutable, and all strings containing the
    same character sequence are equal. Your question does not make sense in
    JavaScript/

    [color=blue]
    > The problem is this: I have an app that reads and modifies JS code in
    > Web pages. In the following code I need to know whether the "src=bar"
    > line operates on the global window.src, or on the local "src" variable
    > in f(), when the "var src" line may or may not be there. Is there any
    > way to do this, short of parsing the function and keeping track of all
    > "var" declarations?
    >
    > src= foo ;
    > function f() {
    > src= bar ;
    > .
    > .
    > .
    > var src ; // may or may not be here
    > }[/color]

    This seems like a very dangerous pattern. Why are you modifying JS code?

    Comment

    • James Marshall

      #3
      Re: Getting a variable reference, or accessing call objects?

      On Sun, 27 Feb 2005, Douglas Crockford wrote:

      DC> > In JavaScript, is there any way to get a reference to a string variable
      DC> > (not an object), like Perl's "\" operator? I want to be able to compare
      DC> > two such references and know if they point to the same string variable.
      DC>
      DC> In JavaScript, all strings are immutable, and all strings containing
      DC> the same character sequence are equal. Your question does not make
      DC> sense in JavaScript/

      It would be one possible solution to my problem.


      DC> > The problem is this: I have an app that reads and modifies JS code in Web
      DC> > pages. In the following code I need to know whether the "src=bar" line
      DC> > operates on the global window.src, or on the local "src" variable in f(),
      DC> > when the "var src" line may or may not be there. Is there any way to do
      DC> > this, short of parsing the function and keeping track of all "var"
      DC> > declarations?
      DC> >
      DC> > src= foo ;
      DC> > function f() {
      DC> > src= bar ;
      DC> > .
      DC> > .
      DC> > .
      DC> > var src ; // may or may not be here
      DC> > }
      DC>
      DC> This seems like a very dangerous pattern. Why are you modifying JS code?

      To make HTML and other resources retrieved through my program retrieve all
      of their respective referred-to resources through the same program. What
      is dangerous about it? What do you mean by "pattern" here?


      ............... ............... ............... ............... ............... ..
      James Marshall james@jmarshall .com Berkeley, CA @}-'-,--
      "Teach people what you know."
      ............... ............... ............... ............... ............... ..

      Comment

      Working...