Get object name from function

Archos

Usenet Archive
Joined
Nov 23, 2011
Messages
39
Reaction score
0
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"
 

Stefan Weiss

Usenet Archive
Joined
Oct 3, 2000
Messages
719
Reaction score
0
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
 

RobG

Usenet Archive
Joined
Aug 21, 2004
Messages
5,448
Reaction score
0
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?
 

Thomas 'PointedEars' Lahn

Usenet Archive
Joined
Oct 14, 2003
Messages
13,491
Reaction score
0
Stefan said:
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
 

Dr J R Stockton

Usenet Archive
Joined
Oct 23, 2006
Messages
3,805
Reaction score
0
In comp.lang.javascript message , Tue, 21 Feb 2012 14:04:38, Archos
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" }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,245
Members
248,133
Latest member
DujonT

Latest Threads

Top