473,786 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I access the object instance from within class

I have a javascript object which dynamically generates a table adding,
deleting and moving rows as the user clicks on buttons or links.
Problem is when I generate a table row and add the javascript method
call to my class, I have to put the object instance name variable of
the class in order for it to be called from the onclick=functio n().
This is seriously limiting, but I'm stuck for a way round it. Heres a
edited of the code so you get the idea....

Instantiating the object :-

var dtl = new DynamicTableLis t("table1", $
{myObject.allFi eldsAsJavaScrip tArray}, true, true, true);
My javascript class DynamicTableLis t, note the dtl javascript object
instance variable being referred to in the addRow function. How can I
avoid this???

function DynamicTableLis t(thisObjName, tableName, options, showDelete,
showUp, showDown)
{
this.processRow = function(r, row, up, down)
{
...
};

this.processRow s = function()
{
...
};

this.getVisibil ity = function(visibl e)
{
...
};

this.delRow = function(button )
{
...
};

this.addRow = function(select ion)
{
...

if (showDelete)
{
var cell2 = document.create Element('TD');
var inp2 = document.create Element('IMG');
/
*************** *************** *************** *************** *************** *************** *******/
inp2.onclick=fu nction(){dtl.de lRow(this);} // Have to specify
dtc!!!!!!!!
/
*************** *************** *************** *************** *************** *************** *******/
inp2.title='Del ete';
inp2.alt='Delet e';
inp2.src='image s/delete.gif';
cell2.appendChi ld(inp2);
row.appendChild (cell2);
}

...

tbody.appendChi ld(row);

this.processRow s();
};

this.moveRow = function(node, vector)
{
};
}

Obviously the code dtl.delRow(this ); is being dynamically generated,
but how do I replace the dtl instance name with something that'll work
whatever the user of this class calls the instance of it!

Cheers, in advance for any help.
Philip Wilkinson.

Apr 22 '07 #1
2 1950
On Apr 22, 8:08 am, wilkinson.phi.. .@gmail.com wrote:
I have a javascript object which dynamically generates a table adding,
deleting and moving rows as the user clicks on buttons or links.
Problem is when I generate a table row and add the javascript method
call to my class, I have to put the object instance name variable of
the class in order for it to be called from the onclick=functio n().
This is seriously limiting, but I'm stuck for a way round it. Heres a
edited of the code so you get the idea....

Instantiating the object :-

var dtl = new DynamicTableLis t("table1", $
{myObject.allFi eldsAsJavaScrip tArray}, true, true, true);

My javascript class DynamicTableLis t, note the dtl javascript object
instance variable being referred to in the addRow function. How can I
avoid this???

function DynamicTableLis t(thisObjName, tableName, options, showDelete,
showUp, showDown)
{
this.processRow = function(r, row, up, down)
{
...
};

this.processRow s = function()
{
...
};

this.getVisibil ity = function(visibl e)
{
...
};

this.delRow = function(button )
{
...
};

this.addRow = function(select ion)
{
...

if (showDelete)
{
var cell2 = document.create Element('TD');
var inp2 = document.create Element('IMG');
/
*************** *************** *************** *************** *************** *************** *******/
inp2.onclick=fu nction(){dtl.de lRow(this);} // Have to specify
dtc!!!!!!!!
/
*************** *************** *************** *************** *************** *************** *******/
I think you want to change the content between the asterisks to the
following two lines.

var thisC = this; // 'this' refers to the instance of DynamicTableLis t
inp2.onclick = function() {thisC.delRow(t his);}; // 'this' referes to
the HTML element that was clicked

The issue here is closures and the binding of the 'this' keyword which
is tricky at first in JavaScript. The 'thisC' variable holds a
reference to the dynamic table list instance. The 'this' in the second
line is resolved at runtime and so point to the HTML element that was
clicked.

If I remember correctly, I heard Brendan Eich, creator of JavaScript,
say that in JavaScript 2.0 the binding of "this" in inner functions
will be the same 'this' as the outer functions. That would mean no
need for the 'thisC' technique above. This will be a backwards
incompatible change and doesn't sound like a good move to me. For
example, how to write the second of the lines I wrote above so that
when delRow() runs it has a reference to the HTML element that was
clicked? It isn't necessarily possible to get it from the event object
because the event's target is the most deeply nested element in DOM
that received the click.

I don't know if the following discusses the 'this' keyword but it is
an article about closures in general

<URL: http://www.jibbering.c om/faq/faq_notes/closures.html>

Peter

Apr 22 '07 #2
Thanks Peter, that worked perfectly and makes total sense.
Cheers, Philip Wilkinson.

Apr 22 '07 #3

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

Similar topics

13
1755
by: Liz | last post by:
ok, this is really simple stuff, or it should be ... but I'm stuck In a Windows Forms app, I have something resembling this: Form1.cs ======== namespace NS Class Form1
1
4472
by: romeo_a_casiple | last post by:
I'm looking to create an static object that has application scope and be able to access that static object from my web service. I understand there are two ways to do this : 1) use the object tag in the global.asax, such as: <object id="objObject" runat="server" class="TestApplication.MyClass" scope="Application" />
12
5556
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
2
1936
by: Corey B | last post by:
Is there a way for an instance of a custom class to access an ASPX page level variable? I know that I can access a Session variable from within a class using the following code: myClassVar = System.Web.HttpContext.Current.Session.Item("mySessionVar") But I can't figure out if it is possible to access a page level variable from within a class.
1
2867
by: Mike Thompson | last post by:
I have an event handler defined within a class. It has the following signature: static void XYZ_EventHandler (object sender, XYZEventArgs e) From within this event handler, I want to access the contents of a textbox on the form that is defined within the default Form1 class. Ordinarily, I would pass the form to any class member function as an argument, but because this is an event handler, I can't pass it.
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.