Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #12009 > unrolled thread

Get object name from function

Started byArchos <raul.san@sent.com>
First post2012-02-21 14:04 -0800
Last post2012-02-22 19:02 +0000
Articles 5 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  Get object name from function Archos <raul.san@sent.com> - 2012-02-21 14:04 -0800
    Re: Get object name from function Stefan Weiss <krewecherl@gmail.com> - 2012-02-21 23:27 +0100
      Re: Get object name from function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-22 11:42 +0100
    Re: Get object name from function RobG <rgqld@iinet.net.au> - 2012-02-21 18:34 -0800
    Re: Get object name from function Dr J R Stockton <reply1208@merlyn.demon.co.uk> - 2012-02-22 19:02 +0000

#12009 — Get object name from function

FromArchos <raul.san@sent.com>
Date2012-02-21 14:04 -0800
SubjectGet object name from function
Message-ID<a256bb7e-3683-43d7-a9c9-086f391db492@m2g2000vbc.googlegroups.com>
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"

[toc] | [next] | [standalone]


#12010

FromStefan Weiss <krewecherl@gmail.com>
Date2012-02-21 23:27 +0100
Message-ID<ji15o1$rte$1@news.albasani.net>
In reply to#12009
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

[toc] | [prev] | [next] | [standalone]


#12039

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-02-22 11:42 +0100
Message-ID<2266507.FRqVoOGUtd@PointedEars.de>
In reply to#12010
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>

[toc] | [prev] | [next] | [standalone]


#12020

FromRobG <rgqld@iinet.net.au>
Date2012-02-21 18:34 -0800
Message-ID<e63374b0-de96-427e-ae54-20dd4cc5ccf6@i10g2000pbc.googlegroups.com>
In reply to#12009
On Feb 22, 8:04 am, Archos <raul....@sent.com> 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

[toc] | [prev] | [next] | [standalone]


#12074

FromDr J R Stockton <reply1208@merlyn.demon.co.uk>
Date2012-02-22 19:02 +0000
Message-ID<tQkk9jGsuTRPFw7V@invalid.uk.co.demon.merlyn.invalid>
In reply to#12009
In comp.lang.javascript message <a256bb7e-3683-43d7-a9c9-086f391db492@m2
g2000vbc.googlegroups.com>, Tue, 21 Feb 2012 14:04:38, Archos
<raul.san@sent.com> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web