Object of a callee

T

tylersimon

Hi,

I am trying to write some javascript in which a method in a custom
object calls a global function. Within this global function I wish to
establish what the object was that called me. I can use the "callee"
method on the function to get the function that was called but I do
not seem to be able to get the object.

Is there a way to do this?

Simon
 
R

RobG

Hi,

I am trying to write some javascript in which a method in a custom
object calls a global function. Within this global function I wish to
establish what the object was that called me. I can use the "callee"
method on the function to get the function that was called but I do
not seem to be able to get the object.

Is there a way to do this?

callee.caller
 
T

Thomas 'PointedEars' Lahn

`callee' is a standardized *non-function property* of the object referred
to by `arguments'. Its use as a property of Function objects would be
proprietary, and therefore unwise; it is not supported in JavaScript 1.5
this way. Its use as a property of the `arguments' property of Function
objects is proprietary and deprecated.
callee.caller

`caller' is a proprietary property of both Function objects and the object
referred to by `arguments'; its use in either way is deprecated since
JavaScript 1.3.

But more important, it does not yield a reference to the object that called
the function (which the OP was actually looking for), but to the Function
object that created the local context from which the function was called.

Therefore, the calling method of the user-defined object should pass its
`this' reference to the global function instead:

function myGlobal(caller)
{
// ...
}

({foo: function() { myGlobal(this); }}).foo();

The alternative is that the global function is called as a method of the
user-defined object, and that the `this' value is used there:

var _global = this;

function myGlobal()
{
if (this != _global)
{
// not called by the Global Object
}
}

({foo: function() { myGlobal.call(this); }}).foo();
or
({foo: function() { myGlobal.apply(this); }}).foo();

It should be noted that global functions are methods of the Global Object.


PointedEars
 

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
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top