473,379 Members | 1,243 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,379 software developers and data experts.

Object references

Hi,

I am a bit new to JavaScript objects, I have the following object:
LinkTest=function() {
// init
}

LinkTest.prototype.eventHandler=function(){
// the call below fails, as 'this' refers to the link that
// generated the event, and not the instance of LinkTest object
this.doSomething();
}

LinkTest.prototype.register=function(link){
link.onclick = this.eventHandler;
}

LinkTest.prototype.doSomething=function() {
// do something!
}

So basically when you register a link, the onclick event handler gets a
reference to
LinkTest's eventHandler function. Now when the user clicks on the link,
the 'this' refers to
the link... how do I reference the instance of LinkTest object from
within the eventHandler function?

Thanks,
Amir

Dec 17 '05 #1
2 1219
Ian
as*******@gmail.com wrote:
Hi,

I am a bit new to JavaScript objects, I have the following object:
LinkTest=function() {
// init
}

LinkTest.prototype.eventHandler=function(){
// the call below fails, as 'this' refers to the link that
// generated the event, and not the instance of LinkTest object
this.doSomething();
}

LinkTest.prototype.register=function(link){
link.onclick = this.eventHandler;
}

LinkTest.prototype.doSomething=function() {
// do something!
}

So basically when you register a link, the onclick event handler gets a
reference to
LinkTest's eventHandler function. Now when the user clicks on the link,
the 'this' refers to
the link... how do I reference the instance of LinkTest object from
within the eventHandler function?

You have to use another level of indirection, create an instance of your
object and have the event handler call its handler function.

linkTest = new LinkTest();

link.onclick = function() { linkTest.eventHandler(); };

Ian
Dec 17 '05 #2
Thanks, that did the trick!

Dec 18 '05 #3

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

Similar topics

6
by: Chris S. | last post by:
I'm trying to make a graphical editor and browser for Pickled files. One aspect I'm not sure about is how to detect multiple references to the same data. For instance, say I had the Pickled...
4
by: Mark D. Anderson | last post by:
About a month ago Richard Cornford did an interesting analysis of a memory leak in jscript (internet explorer) when there are "circular" references between DOM objects and (real) jscript objects:...
44
by: Steven T. Hatton | last post by:
This may seem like such a simple question, I should be embarrassed to ask it. The FAQ says an object is "A region of storage with associated semantics." OK, what exactly is meant by "associated...
3
by: RobG | last post by:
I am playing with a script that will allow columns of a table to be moved by dragging them left or right. To do this with functions is fairly straight forward, however after looking at Richard...
5
by: Robert Zurer | last post by:
I have a large pool of business objects all referencing one another in various ways. In the client application I want to do something like employee.Delete(); Behind the scenes, I want to...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
28
by: Stef Mientki | last post by:
hello, I'm trying to build a simple functional simulator for JAL (a Pascal-like language for PICs). My first action is to translate the JAL code into Python code. The reason for this approach is...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.