Getting a variable reference, or accessing call objects?

J

James Marshall

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 ;
 
D

Douglas Crockford

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.

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

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
}

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

James Marshall

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 (e-mail address removed) Berkeley, CA @}-'-,--
"Teach people what you know."
.............................................................................
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top