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


Groups > comp.lang.javascript > #12039

Re: Get object name from function

Message-ID <2266507.FRqVoOGUtd@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-02-22 11:42 +0100
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

Followups directed to: comp.lang.javascript

Show all headers | 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