473,505 Members | 14,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass Initialized Object Reference to Event

If I initializer my object as

var obj = new SomeObject();

then I have some method inside of the object as:

function SomeObject(){
.......
this.showLink = function(){
return "<a href='javascript:void(0)' onClick='obj.showMessage()'></
a>"
}
this.showMessage = function(){ alert("My Message is Here") }
.......
}

Obviously, "onClick='obj.showMessage()' " is a wrong refrence because
we may call it obj2 or have multiple objects. How can I reference any
function of an initialized object on click of a link?
Thank you for suggestions.
Jun 27 '08 #1
1 1366
vunet wrote:
If I initializer my object as

var obj = new SomeObject();

then I have some method inside of the object as:

function SomeObject(){
......
this.showLink = function(){
return "<a href='javascript:void(0)' onClick='obj.showMessage()'></
a>"
}
this.showMessage = function(){ alert("My Message is Here") }
......
}

Obviously, "onClick='obj.showMessage()' " is a wrong refrence because
we may call it obj2 or have multiple objects. How can I reference any
function of an initialized object on click of a link?
Don't use strings. And move all methods that don't require a closure to the
prototype object.

function isMethod(o, p)
{
return (o && /\s*(function|object|unknown)\s*/i.test(typeof o[p])
&& o[p]);
}

function SomeObject()
{
this.showLink = function() {
if (isMethod(document, "createElement"))
{
var a = document.createElement("a");

if (a)
{
a.href = "javascript:void(0)";

var me = this;
var f = function(e) {
me.showMessage();

if (isMethod(e, "preventDefault")) e.preventDefault();
if (typeof e.returnValue != "undefined") e.returnValue = false;
return false;
};

if (isMethod(a, "addEventListener"))
{
a.addEventListener("click", f, false);
}
else
{
a.onclick = f;
}
}
}

return a;
};
}

SomeObject.prototype = {
constructor: SomeObject,
showMessage: function(){ alert("My Message is Here") }
};

That said, don't misuse `a' elements like this.
Thank you for suggestions.
You're welcome.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jun 27 '08 #2

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

Similar topics

4
4716
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
110
9812
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
5
11028
by: deko | last post by:
I'd like to use a bit of code in the OnOpen event of a report: =rptOpen(Me.ReportName), (Me.Tag) --this doesn't work This does work: Private Sub Report_Open(Cancel As Integer)...
7
2376
by: John | last post by:
Hi I open a word document from my vb.net app. Now I want to pass reference to one of the classes in my vb.net app to word so it can access procedures in the vb.net class. How can I pass the...
3
2220
by: sloan | last post by:
How does one "pass thru" a Raised Event.... I am using the Adapter Pattern to sync up some different interfaces. http://www.dofactory.com/Patterns/PatternAdapter.aspx My Question is this:
14
20369
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
11
3333
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
9
2052
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen...
24
55033
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
0
7216
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,...
0
7303
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,...
0
7367
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...
1
7018
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...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4699
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...
0
3187
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...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.