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. Advertisements

  2. Archos

    Stefan Weiss Guest

    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
    1. Advertisements

  3. Archos

    RobG Guest

    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?
    RobG, Feb 22, 2012
  4. No, a *reference to* the resulting *Array instance* will be passed to

    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, [], 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.)

    Thomas 'PointedEars' Lahn, Feb 22, 2012
  5. In comp.lang.javascript message <a256bb7e-3683-43d7-a9c9-086f391db492@m2>, Tue, 21 Feb 2012 14:04:38, Archos
    Very readily. If the argument is an Object, just access its name

    FuncTest(Ob) { alert( }
    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" }
    Dr J R Stockton, Feb 22, 2012
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.