Determine object from property?

M

mikeorb

Is there a way to determine the object that a property is in? For
example:

function MyClass() { ... }
MyClass.prototype.myFunc = function() { ... }

var obj = new MyClass();
var sameobj = SOMEFUNCTION(obj.myFunc);

So my goal is to get a reference to the object by just having the
property.

I think the answer is NO but I wanted to be sure there was not sime
built-in function that provided this.

Thanks,
-Mike
 
B

Baconbutty

mikeorb said:
Is there a way to determine the object that a property is in? For
example:

By "property", you mean "method" (or Function) in your example.
function MyClass() { ... }
MyClass.prototype.myFunc = function() { ... }

var obj = new MyClass();
var sameobj = SOMEFUNCTION(obj.myFunc);

The example you give seems a little odd, in that you already have a
reference to "obj" in your variable name "obj", so why do you need a
function to get it again? Could you explain the application/context a
little more?
So my goal is to get a reference to the object by just having the
property.

For the purposes of this answer, I assume that you mean that if you
just pass a reference to "myFunc", can the receiving function guess
which instance of MyClass it came from. The answer is NO generally,
unless you use closures.

My answer is fairly amateur/simplified, so I hope it makes sense.

1 FUNCTIONS AS METHODS

In javascript, whenever you call a Javascript Function, it is always
being called as a "method" of some instance of an object.

You can discover which object is calling the function, through the
"this" keyword inside the function.

2 METHODS OF WINDOW OBJECT BY DEFAULT

The default is that a Function is called as a method of the "window"
object.

Thus:-

function MyFunction()
{
alert((this===window));
}

MyFunction(); // alerts true, "window" is implicit
window.MyFunction(); // explicit
window["MyFunction"](); // explicit

3. METHODS OF USER OBJECT

If you add a Function as a method of your own object, then the "this"
keyword will refer to the instance of that object, ONLY WHEN IT IS
CALLED AS A METHOD OF THAT OBJECT.

Thus:-

function MyClass(a)
{
this.a=a;
}
MyClass.prototype.myFunc=function(){return this;};

function fTest()
{
var inst=new MyClass(1);
var sameinst=inst.myFunc();
alert(sameinst.a);
}

However, if you get a reference to "myFunc" only, then "this" will
refer to the window object.

function fTest()
{
var inst=new MyClass(1);
var funcref=inst.myFunc;
alert((funcref()===window)); // true
}

4. CLOSURES

One way to achieve your goal may be to use closures. There are plenty
of posts in this news group on closures and scope chains if you search
it.

A closure is a means by which you can bind an externally declared
variable into a function. Effectively "global" variables are an
example of a closure at the top level of your script.

Thus:=

function MyClass(a)
{
var oInst=this; // oInst is effectively global to "myFunc"

this.a=a;

this.myFunc=function(){
return oInst;
}
}

function fTest()
{
var inst=new MyClass(1);
var funcref=inst.myFunc;
alert(funcref().a);
}

Hope this helps.
 
L

Lasse Reichstein Nielsen

mikeorb said:
Is there a way to determine the object that a property is in?

No.

There is no way to refer progammatically to a property, only to its
value[1], and that value can be a property of many objects.
var obj = new MyClass();

Insert
var otherObj = {myFunc: obj.myFunc};
var sameobj = SOMEFUNCTION(obj.myFunc);

Then the value of "obj.myFunc" is a property of both "obj" and
"otherObj".
I think the answer is NO but I wanted to be sure there was not sime
built-in function that provided this.

There isn't.
/L
[1] Actually, an expression like "obj.myFunc" evaluates to a "Reference"
in the specification of ECMAScript, and a Reference does contain both
the object and the property. It is used for assigning the "this" value
when calling a method. There's just no way of getting to the object except
calling the method.
 
M

mikeorb

The example you give seems a little odd, in that you already have a
reference to "obj" in your variable name "obj", so why do you need a
function to get it again? Could you explain the application/context a
little more?

I created a simple closure wrapper:

// Usage: var func = Method2Func(obj, method, [ static arguments to
method ]);
// func([dynamic arguments to method]);
//
// Example:
// var o = new MyClass();
// var hello = Method2Func(o, o.Hello, "static");
// hello("dynamic"); // Invokes o.Hello("static", "dynamic")
//
// Creates an anonymous function that, when called, invokes obj.func
and passes
// it the optional arguments you supply. There are two types of
arguments:
//
// 1) Static that are always passed. They are supplied in the
Method2Func
// call and are obviously evaluated when the function reference is
// created.
// 2) Dynamic arguments that are passed when you invoke the function.
These
// get appended to the static arguments.
function Method2Func(obj, func)
{
var static_args = [];
for (i = 2; i < arguments.length; ++i)
{
static_args.push(arguments);
}
return function()
{
var args = Array.concat(static_args, arguments);
func.apply(obj, args);
}
}

So I'd rather be able to say Method2Func(this.method) than
Method2Fun(this, this.method).

Oops. Uses this static method:

// Usage: var new_array = Array.concat(array1, array2, ...)
Array.concat = function()
{
var array = [];
for (i = 0; i < arguments.length; i++)
{
var sub_array = arguments;
for (j = 0; j < sub_array.length; j++)
{
array.push(sub_array[j]);
}
}
return array;
}

-Mike
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top