how to determine if an object exists in the CALL object

Discussion in 'Javascript' started by George, Oct 3, 2009.

  1. George

    George Guest

    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
     
    George, Oct 3, 2009
    #1
    1. Advertising

  2. George

    RobG Guest

    On Oct 3, 2:28 pm, George <> 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
     
    RobG, Oct 3, 2009
    #2
    1. Advertising

  3. 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
     
    Thomas 'PointedEars' Lahn, Oct 3, 2009
    #3
  4. George

    George Guest

    On Oct 3, 9:53 am, Hans-Georg Michna <hans-
    > 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
     
    George, Oct 3, 2009
    #4
  5. George

    George Guest

    On Oct 3, 5:09 am, RobG <> wrote:
    > On Oct 3, 2:28 pm, George <> 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
     
    George, Oct 3, 2009
    #5
  6. 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
     
    Thomas 'PointedEars' Lahn, Oct 3, 2009
    #6
  7. George wrote:

    > On Oct 3, 5:09 am, RobG <> wrote:
    >> On Oct 3, 2:28 pm, George <> 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$>
     
    Thomas 'PointedEars' Lahn, Oct 3, 2009
    #7
  8. George wrote:

    > On Oct 3, 5:09 am, RobG <> wrote:
    >> On Oct 3, 2:28 pm, George <> 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$>
     
    Thomas 'PointedEars' Lahn, Oct 3, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?T3BpZQ==?=

    Determine if record exists

    =?Utf-8?B?T3BpZQ==?=, Feb 12, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    8,203
    Joe Fallon
    Feb 13, 2005
  2. Shapper
    Replies:
    4
    Views:
    33,210
  3. Totan
    Replies:
    0
    Views:
    1,058
    Totan
    Apr 17, 2006
  4. Ulf Meinhardt
    Replies:
    8
    Views:
    6,264
  5. JT

    determine if a form object exists

    JT, Jun 30, 2004, in forum: ASP General
    Replies:
    2
    Views:
    162
Loading...

Share This Page