Determine object from property?

Discussion in 'Javascript' started by mikeorb, Sep 9, 2005.

  1. mikeorb

    mikeorb Guest

    Is there a way to determine the object that a property is in? For
    example:

    function MyClass() { ... }
    MyClass.prototype.myFunc = function() { ... }

    var obj = new MyClass();
    var sameobj = SOMEFUNCTION(obj.myFunc);

    So my goal is to get a reference to the object by just having the
    property.

    I think the answer is NO but I wanted to be sure there was not sime
    built-in function that provided this.

    Thanks,
    -Mike
    mikeorb, Sep 9, 2005
    #1
    1. Advertising

  2. mikeorb

    Baconbutty Guest

    mikeorb wrote:
    > Is there a way to determine the object that a property is in? For
    > example:


    By "property", you mean "method" (or Function) in your example.

    > function MyClass() { ... }
    > MyClass.prototype.myFunc = function() { ... }
    >
    > var obj = new MyClass();
    > var sameobj = SOMEFUNCTION(obj.myFunc);


    The example you give seems a little odd, in that you already have a
    reference to "obj" in your variable name "obj", so why do you need a
    function to get it again? Could you explain the application/context a
    little more?

    > So my goal is to get a reference to the object by just having the
    > property.


    For the purposes of this answer, I assume that you mean that if you
    just pass a reference to "myFunc", can the receiving function guess
    which instance of MyClass it came from. The answer is NO generally,
    unless you use closures.

    My answer is fairly amateur/simplified, so I hope it makes sense.

    1 FUNCTIONS AS METHODS

    In javascript, whenever you call a Javascript Function, it is always
    being called as a "method" of some instance of an object.

    You can discover which object is calling the function, through the
    "this" keyword inside the function.

    2 METHODS OF WINDOW OBJECT BY DEFAULT

    The default is that a Function is called as a method of the "window"
    object.

    Thus:-

    function MyFunction()
    {
    alert((this===window));
    }

    MyFunction(); // alerts true, "window" is implicit
    window.MyFunction(); // explicit
    window["MyFunction"](); // explicit

    3. METHODS OF USER OBJECT

    If you add a Function as a method of your own object, then the "this"
    keyword will refer to the instance of that object, ONLY WHEN IT IS
    CALLED AS A METHOD OF THAT OBJECT.

    Thus:-

    function MyClass(a)
    {
    this.a=a;
    }
    MyClass.prototype.myFunc=function(){return this;};

    function fTest()
    {
    var inst=new MyClass(1);
    var sameinst=inst.myFunc();
    alert(sameinst.a);
    }

    However, if you get a reference to "myFunc" only, then "this" will
    refer to the window object.

    function fTest()
    {
    var inst=new MyClass(1);
    var funcref=inst.myFunc;
    alert((funcref()===window)); // true
    }

    4. CLOSURES

    One way to achieve your goal may be to use closures. There are plenty
    of posts in this news group on closures and scope chains if you search
    it.

    A closure is a means by which you can bind an externally declared
    variable into a function. Effectively "global" variables are an
    example of a closure at the top level of your script.

    Thus:=

    function MyClass(a)
    {
    var oInst=this; // oInst is effectively global to "myFunc"

    this.a=a;

    this.myFunc=function(){
    return oInst;
    }
    }

    function fTest()
    {
    var inst=new MyClass(1);
    var funcref=inst.myFunc;
    alert(funcref().a);
    }

    Hope this helps.
    Baconbutty, Sep 9, 2005
    #2
    1. Advertising

  3. "mikeorb" <> writes:

    > Is there a way to determine the object that a property is in?


    No.

    There is no way to refer progammatically to a property, only to its
    value[1], and that value can be a property of many objects.

    > var obj = new MyClass();


    Insert
    var otherObj = {myFunc: obj.myFunc};

    > var sameobj = SOMEFUNCTION(obj.myFunc);


    Then the value of "obj.myFunc" is a property of both "obj" and
    "otherObj".

    > I think the answer is NO but I wanted to be sure there was not sime
    > built-in function that provided this.


    There isn't.
    /L
    [1] Actually, an expression like "obj.myFunc" evaluates to a "Reference"
    in the specification of ECMAScript, and a Reference does contain both
    the object and the property. It is used for assigning the "this" value
    when calling a method. There's just no way of getting to the object except
    calling the method.
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Sep 9, 2005
    #3
  4. mikeorb

    mikeorb Guest

    > The example you give seems a little odd, in that you already have a
    > reference to "obj" in your variable name "obj", so why do you need a
    > function to get it again? Could you explain the application/context a
    > little more?


    I created a simple closure wrapper:

    // Usage: var func = Method2Func(obj, method, [ static arguments to
    method ]);
    // func([dynamic arguments to method]);
    //
    // Example:
    // var o = new MyClass();
    // var hello = Method2Func(o, o.Hello, "static");
    // hello("dynamic"); // Invokes o.Hello("static", "dynamic")
    //
    // Creates an anonymous function that, when called, invokes obj.func
    and passes
    // it the optional arguments you supply. There are two types of
    arguments:
    //
    // 1) Static that are always passed. They are supplied in the
    Method2Func
    // call and are obviously evaluated when the function reference is
    // created.
    // 2) Dynamic arguments that are passed when you invoke the function.
    These
    // get appended to the static arguments.
    function Method2Func(obj, func)
    {
    var static_args = [];
    for (i = 2; i < arguments.length; ++i)
    {
    static_args.push(arguments);
    }
    return function()
    {
    var args = Array.concat(static_args, arguments);
    func.apply(obj, args);
    }
    }

    So I'd rather be able to say Method2Func(this.method) than
    Method2Fun(this, this.method).

    Oops. Uses this static method:

    // Usage: var new_array = Array.concat(array1, array2, ...)
    Array.concat = function()
    {
    var array = [];
    for (i = 0; i < arguments.length; i++)
    {
    var sub_array = arguments;
    for (j = 0; j < sub_array.length; j++)
    {
    array.push(sub_array[j]);
    }
    }
    return array;
    }

    -Mike
    mikeorb, Sep 9, 2005
    #4
    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. Eric Newton
    Replies:
    3
    Views:
    9,342
    Brock Allen
    Apr 4, 2005
  2. mrdylan
    Replies:
    3
    Views:
    290
    Steven D'Aprano
    Apr 12, 2006
  3. TS
    Replies:
    3
    Views:
    2,453
  4. Peña, Botp
    Replies:
    1
    Views:
    221
    Robert Klemme
    Jan 24, 2004
  5. George
    Replies:
    7
    Views:
    145
    Thomas 'PointedEars' Lahn
    Oct 3, 2009
Loading...

Share This Page