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


Groups > comp.lang.javascript > #12039

Re: Get object name from function

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.chainon-marquant.org!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <2266507.FRqVoOGUtd@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Wed, 22 Feb 2012 11:42:49 +0100
User-Agent KNode/4.4.11
Content-Transfer-Encoding 8Bit
Subject Re: Get object name from function
Newsgroups comp.lang.javascript
References <a256bb7e-3683-43d7-a9c9-086f391db492@m2g2000vbc.googlegroups.com> <ji15o1$rte$1@news.albasani.net>
Followup-To comp.lang.javascript
MIME-Version 1.0
Lines 63
NNTP-Posting-Date 22 Feb 2012 11:42:49 CET
NNTP-Posting-Host 8b8f83a3.newsspool2.arcor-online.net
X-Trace DXC=C3eUhX^B7@OA<A_d>@P]\DA9EHlD;3YcB4Fo<]lROoRA8kF<OcfhCOKL9HoAM`Ej_ODZm8W4\YJNLUFDj8T]K0?CDmniOZG;K0KL8k[;d4?4iF
X-Complaints-To usenet-abuse@arcor.de
Xref x330-a1.tempe.blueboxinc.net comp.lang.javascript:12039

Followups directed to: comp.lang.javascript

Show key headers only | View raw


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>

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web