Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > how to determine if an object exists in the CALL object

Reply
Thread Tools

how to determine if an object exists in the CALL object

 
 
George
Guest
Posts: n/a
 
      10-03-2009
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
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      10-03-2009
On Oct 3, 2:28*pm, George <(E-Mail Removed)> wrote:
> 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.


--
Rob
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-03-2009
George wrote:

> 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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      10-03-2009
On Oct 3, 9:53*am, Hans-Georg Michna <hans-
(E-Mail Removed)> wrote:
> On Fri, 2 Oct 2009 21:28:44 -0700 (PDT), George wrote:
> >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
> >}

>
> 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
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      10-03-2009
On Oct 3, 5:09*am, RobG <(E-Mail Removed)> wrote:
> On Oct 3, 2:28*pm, George <(E-Mail Removed)> wrote:
>
> > 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.
>
> --
> Rob


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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-03-2009
George wrote:

> Hans-Georg Michna wrote:
>> 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.


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
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-03-2009
George wrote:

> On Oct 3, 5:09 am, RobG <(E-Mail Removed)> wrote:
>> On Oct 3, 2:28 pm, George <(E-Mail Removed)> wrote:
>> 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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-03-2009
George wrote:

> On Oct 3, 5:09 am, RobG <(E-Mail Removed)> wrote:
>> On Oct 3, 2:28 pm, George <(E-Mail Removed)> wrote:
>> 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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to check if a directory exists? folder.exists() does not work! Ulf Meinhardt Java 8 08-28-2009 12:26 PM
finding if file exists or not in apache webdav server and if exists what is the mime type of it.... Totan Java 0 04-17-2006 05:13 AM
Determine if record exists =?Utf-8?B?T3BpZQ==?= ASP .Net 6 02-13-2005 06:09 AM
determine if a form object exists JT ASP General 2 07-02-2004 07:37 PM



Advertisments