Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Get object name from function

Reply
Thread Tools

Get object name from function

 
 
Archos
Guest
Posts: n/a
 
      02-21-2012
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,) // should get: "array"
 
Reply With Quote
 
 
 
 
Stefan Weiss
Guest
Posts: n/a
 
      02-21-2012
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,) // should get: "array"


No, that's not possible. array.slice(4, will be evaluated first, and
the resulting array will be passed to funcTest.

- stefan
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      02-22-2012
On Feb 22, 8:04*am, Archos <(E-Mail Removed)> 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,) // 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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-22-2012
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,) // should get: "array"

>
> No, that's not possible. array.slice(4, 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, );

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, );

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, );
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>
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      02-22-2012
In comp.lang.javascript message <a256bb7e-3683-43d7-a9c9-086f391db492@m2
g2000vbc.googlegroups.com>, Tue, 21 Feb 2012 14:04:38, Archos
<(E-Mail Removed)> 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,) // 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call a function by a string of name & How to get the name of aclass? cutecutemouse C++ 2 07-13-2008 09:26 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
get object name from within object warteschlange Javascript 17 07-25-2005 12:40 PM
how to get name of function from within function? Christopher J. Bottaro Python 16 06-06-2005 06:50 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments