Get object name from function

Discussion in 'Javascript' started by Archos, Feb 21, 2012.

  1. Archos

    Archos Guest

    If if is passed an object with a method to a function, would be
    possible to get the name of that object from the function)

    funcTest(array.slice(4,8)) // should get: "array"
     
    Archos, Feb 21, 2012
    #1
    1. Advertising

  2. Archos

    Stefan Weiss Guest

    On 2012-02-21 23:04, Archos wrote:
    > If if is passed an object with a method to a function, would be
    > possible to get the name of that object from the function)
    >
    > funcTest(array.slice(4,8)) // should get: "array"


    No, that's not possible. array.slice(4,8) will be evaluated first, and
    the resulting array will be passed to funcTest.

    - stefan
     
    Stefan Weiss, Feb 21, 2012
    #2
    1. Advertising

  3. Archos

    RobG Guest

    On Feb 22, 8:04 am, Archos <> wrote:
    > If if is passed an object with a method to a function, would be
    > possible to get the name of that object from the function)
    >
    > funcTest(array.slice(4,8)) // should get: "array"


    In the above, the result of array.slice(...) is a new (anonymous)
    array, not the one referenced by *array*.

    Further, the called function has no clue about the expression that
    created the value. All it has is a reference to it as arguments[0] and
    possibly also using the identifier assigned to the first formal
    parameter if there is one. It doesn't know that the object referenced
    by arguments[0] was created in some other context from *array*.

    When an object (such as an array) is assigned to a variable, the value
    of the variable is a reference to the object. The object has no clue
    about the variables that reference it — there might be two, three or
    ten different variables all refencing the same object. What is its
    "name" in that case?


    --
    Rob
     
    RobG, Feb 22, 2012
    #3
  4. Stefan Weiss wrote:

    > On 2012-02-21 23:04, Archos wrote:
    >> If if is passed an object with a method to a function, would be
    >> possible to get the name of that object from the function)
    >>
    >> funcTest(array.slice(4,8)) // should get: "array"

    >
    > No, that's not possible. array.slice(4,8) will be evaluated first, and
    > the resulting array will be passed to funcTest.


    No, a *reference to* the resulting *Array instance* will be passed to
    funcTest().

    This is important: Objects do _not_ have names; they have *identity*.
    Objects are *never* accessed directly in ECMAScript implementations; they
    are *referred to*. The same object can be referred to by *several*
    properties of *several other* objects (the base object in that network of
    objects, properties and references is the Global Object).

    Therefore, if you want to manipulate an Array instance and require
    additional arguments, pass the reference to that instance, through a
    property name if available (variable identifiers are property names), and
    the additional arguments:

    function funcTest(objRef, arg1, arg2)
    {
    return objRef.slice(arg1, arg);
    }

    console.log(funcTest(a, 4, 8));

    Since property names are strings, you can even pass the method name:

    function funcTest(objRef, methodName, arg1, arg2)
    {
    return objRef[methodName](arg1, arg);
    }

    console.log(funcTest(a, "slice", 4, 8));

    Or, even more flexible:

    function funcTest(objRef, methodName, args)
    {
    return objRef[methodName].apply(objRef, [].slice.call(arguments, 2));
    }

    console.log(funcTest(a, "toString"));
    console.log(funcTest(a, "push", 42));
    console.log(funcTest(a, "slice", 4, 8));
    console.log(funcTest(a, "splice", 4, 1, 42));
    // …

    (`args' is used here for illustration only; the declaration is not needed.)


    PointedEars
    --
    Sometimes, what you learn is wrong. If those wrong ideas are close to the
    root of the knowledge tree you build on a particular subject, pruning the
    bad branches can sometimes cause the whole tree to collapse.
    -- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>
     
    Thomas 'PointedEars' Lahn, Feb 22, 2012
    #4
  5. In comp.lang.javascript message <a256bb7e-3683-43d7-a9c9-086f391db492@m2
    g2000vbc.googlegroups.com>, Tue, 21 Feb 2012 14:04:38, Archos
    <> posted:

    >If if is passed an object with a method to a function, would be
    >possible to get the name of that object from the function)


    Very readily. If the argument is an Object, just access its name
    property.

    FuncTest(Ob) { alert(Ob.name) }

    >funcTest(array.slice(4,8)) // should get: "array"


    But, from that, you seem to want its identifier.

    That is possible if, and AFAIR only if, the argument is a function that
    has been declared in the common manner :

    function IDofFunc(Fn) { // Fn is a function; return its name
    var A = Fn.toString().match(/function\s+(\w+)\(/)
    return A ? A[1] : "anon" }

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
    Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
    Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr J R Stockton, Feb 22, 2012
    #5
    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. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    890
    Roedy Green
    Jan 2, 2006
  2. ding feng
    Replies:
    2
    Views:
    2,907
    ding feng
    Jun 25, 2003
  3. Christopher J. Bottaro

    how to get name of function from within function?

    Christopher J. Bottaro, Jun 3, 2005, in forum: Python
    Replies:
    16
    Views:
    581
    Christopher J. Bottaro
    Jun 6, 2005
  4. cutecutemouse
    Replies:
    2
    Views:
    387
    cutecutemouse
    Jul 13, 2008
  5. warteschlange

    get object name from within object

    warteschlange, Jul 22, 2005, in forum: Javascript
    Replies:
    17
    Views:
    294
    Dr John Stockton
    Jul 25, 2005
Loading...

Share This Page