how to determine if an object exists in the CALL object

G

George

Hi,

I am wondering what is the grace way to determine whether an object
exists in the current function Call object or not. Namely, how to
determine if an object is locally defined or not. For example, in the
following script, how to implement the function IsDefinedLocally?

var x = {a:1};
function f() {
var ref;

ref = x;
IsDefinedLocally(ref); //should return false

var loc_obj = {a:1};
ref = loc_obj;
IsDefinedLocally(ref); //should return true
}

Thanks,
Yan Huang
 
R

RobG

Hi,

I am wondering what is the grace way to determine whether an object
exists in the current function Call object or not.

Where "exists" has some special meaning to you that you don't explain.

Namely, how to
determine if an object is locally defined or not. For example, in the
following script, how to implement the function IsDefinedLocally?

var x = {a:1};

Here, x is declared globally. Then, when that line of code is
executed, it is assigned a reference to an object.
function f() {
  var ref;

Here ref is declared as a local variable of the function f.
  ref = x;

When f is called, ref is assigned a reference to the same object that
as was assigned to x.
  IsDefinedLocally(ref);  //should return false

ref was *declared* locally. Whether the object that it now references
was fist assigned to a global variable (or one defined in any other
scope) is impossible to determine. You haven't explained why you want
to know that.
  var loc_obj = {a:1};
  ref = loc_obj;
  IsDefinedLocally(ref);  //should return true

As above, replacing global with local.

It would be best to explain what you are trying to do, then it might
be possible to suggest a solution.
 
T

Thomas 'PointedEars' Lahn

George said:
I am wondering what is the grace way to determine whether an object
exists in the current function Call object or not.

Objects do not have existence or position with regard to the source code.
They are stored (they exist) in memory, and are only referred to by tokens
in the source code.
Namely, how to
determine if an object is locally defined or not. For example, in the
following script, how to implement the function IsDefinedLocally?

var x = {a:1};
function f() {
var ref;

ref = x;
IsDefinedLocally(ref); //should return false

var loc_obj = {a:1};
ref = loc_obj;
IsDefinedLocally(ref); //should return true
}

You cannot, as the Variable Object of the local execution context (of which
a declared variable -- e.g. `ref' -- would be a property) cannot be accessed
(else you could use the `in' operator, with caveats).


PointedEars
 
G

George

Since that is not directly possible, let me make a ridiculously
trivial solution. Declare the scope while you are creating the
variable:

var x = { a: 1, isDefinedLocally: false };
function f() {
  var ref;

  ref = x;
  ref.isDefinedLocally; //should return false

  var loc_obj = { a: 1, isDefinedLocally: true };
  ref = loc_obj;
  ref.isDefinedLocally; //should return true

}

If, however, you want to do this with variables defined in
foreign code, which you cannot, or don't want to, modify, then
there is no straightforward solution.

Hans-Georg

Yes, this was also the approach I came up with. I am just willingly
feeling it might be a better solution leveraging some existing JS
language features.

Thanks,
Yan Huang
 
G

George

Where "exists" has some special meaning to you that you don't explain.



Here, x is declared globally. Then, when that line of code is
executed, it is assigned a reference to an object.


Here ref is declared as a local variable of the function f.




When f is called, ref is assigned a reference to the same object that
as was assigned to x.


ref was *declared* locally. Whether the object that it now references
was fist assigned to a global variable (or one defined in any other
scope) is impossible to determine. You haven't explained why you want
to know that.




As above, replacing global with local.




It would be best to explain what you are trying to do, then it might
be possible to suggest a solution.

I'd like to decide the set of live-out objects for a function. An
object is said to be live-out with respect to a function if its value
can be affected by the computation of the function, i.e., the
execution of the function drops side effects to the live-out object.

Certainly, there is another way side effects appear:

var x = 1;
function f() {
var loc_x = 100;

x++; // I would only worry about this update.
loc_x++; // I would not worry about effects confined within the
current Call Object.
}

It seem that what I really need to take care with is the *base* of
every reference (the reference type defined internally for JS in
ECMAScript specs).

Thanks,
Yan Huang
 
T

Thomas 'PointedEars' Lahn

But there is.

var x = { ... };

function f()
{
var ident = {};
var ref = x;

/* always false */
ref.ident === ident;

var obj = {a: 1, ident: ident};

/* true */
obj.ident === ident;
}
Yes, this was also the approach I came up with. I am just willingly
feeling it might be a better solution leveraging some existing JS
language features.

Looks like a solution looking for a problem. Why would you want to do this?


PointedEars
 
T

Thomas 'PointedEars' Lahn

George said:
It would be best to explain what you are trying to do, then it might
be possible to suggest a solution.
[...]

Please trim your quotes to the relevant parts; usually do not quote sigs.
I'd like to decide the set of live-out objects for a function. An
object is said to be live-out with respect to a function if its value
can be affected by the computation of the function, i.e., the
execution of the function drops side effects to the live-out object.

As explained before, your notion of "live-out objects" is ill-advised.
Almost any previously created global object can be referred to from any
local execution context.
Certainly, there is another way side effects appear:

var x = 1;
function f() {
var loc_x = 100;

x++; // I would only worry about this update.
loc_x++; // I would not worry about effects confined within the
current Call Object.
}

It seem that what I really need to take care with is the *base* of
every reference (the reference type defined internally for JS in
ECMAScript specs).

Your assumption does not make sense; `x' and `loc_x' do not store a
reference to an object here. And, in fact, if you do not know which
identifiers you have declared locally, you are in greater trouble
than you think.


PointedEars
 
T

Thomas 'PointedEars' Lahn

George said:
It would be best to explain what you are trying to do, then it might
be possible to suggest a solution.
[...]

Please trim your quotes to the relevant parts; usually do not quote sigs.
I'd like to decide the set of live-out objects for a function. An
object is said to be live-out with respect to a function if its value
can be affected by the computation of the function, i.e., the
execution of the function drops side effects to the live-out object.

As explained before, your notion of "live-out objects" is ill-advised.
Almost any object previously created by executing code in an outer execution
context can be referred to by code in an inner execution context.
Certainly, there is another way side effects appear:

var x = 1;
function f() {
var loc_x = 100;

x++; // I would only worry about this update.
loc_x++; // I would not worry about effects confined within the
current Call Object.
}

It seem that what I really need to take care with is the *base* of
every reference (the reference type defined internally for JS in
ECMAScript specs).

Your assumption does not make sense; `x' and `loc_x' do not store a
reference to an object here. And, in fact, if you do not know which
identifiers you have declared locally, you are in greater trouble
than you think.


PointedEars
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top