473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Determine object from property?

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

Sep 9 '05 #1
3 1665
mikeorb wrote:
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.

Sep 9 '05 #2
"mikeorb" <mi*********@orb.dreamhost.com> writes:
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.
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 9 '05 #3
> 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[i]);
}
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[i];
for (j = 0; j < sub_array.length; j++)
{
array.push(sub_array[j]);
}
}
return array;
}

-Mike

Sep 9 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: crjunk | last post by:
I have a script that I want to run only when my input box IS NOT disabled. Can someone tell me if something is wrong with my script? Is "disabled" the correct property to use? function...
9
by: Adam | last post by:
Can someone please help!! I am trying to figure out what a font is? Assume I am working with a fixed font say Courier 10 point font Question 1: What does this mean 10 point font Question 2:...
3
by: BBFrost | last post by:
Ok, I know how to count the number of selected datagrid rows using the code below. What has me stumped is how to determine when the selected rows within a datagrid have been changed. The...
2
by: jmhmaine | last post by:
I've created a lookup class for a windows form application and I'm stuck on how on using the collection object. The process is simple, if the item is in the collection then return that object, if...
3
by: Developer in California | last post by:
I am working on developing a generic Web framework using Master Pages in ASP.NET 2.0. What I have done is created a PageRenderer class which has a public method which will retrieve the path of the...
3
by: mrdylan | last post by:
Given a class like so: ------------------------------- class TestMe(object): def get(self): pass def set(self, v): pass p = property( get, set )
8
by: Ole | last post by:
If I define a class and create a instant of it like e.g.: UserClass instantName = new UserClass(); how do I then determine the defined name "instantName" in the UserClass e.g. in a method (or...
7
by: semedao | last post by:
Hi all, I view many posts about this issue , the connected property does not tell us the current status of the socket. based on couple of suggestions of msdn , and some article here , I try to...
2
by: =?Utf-8?B?UmljaA==?= | last post by:
I have code to bold text in a datagridviewcell: Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim cs As DataGridViewCellStyle, fnt...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.