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> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn 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: 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@P]\DA9EHlD;3YcB4Fo<]lROoRA8kF 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,