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

JS class + events

Hello all.
Im writing a class for context menus in web pages. I have problem with
accessing properties of my class from event function. To understand my
question i will paste a test code.

[script]
function myClass() {
this.a = 'test1';
this.b = 'test2';

this.MDown = function () {
alert(this.a); // I need to access variable a from myClass. But this
return error because "this" in this case refers to the HTML element the
event is handled by
}

document.addEventListener('mousedown', this.MDown, false);
}

var a = new myClass();
[/script]
Edit/Delete Message

Sep 20 '06 #1
1 1585

TheC0der wrote:
Hello all.
Im writing a class for context menus in web pages. I have problem with
accessing properties of my class from event function. To understand my
question i will paste a test code.
It helps greatly if you indent your code to make it easy to read:

function myClass() {
this.a = 'test1';
this.b = 'test2';

this.MDown = function () {
alert(this.a);
/* I need to access variable a from myClass. But this return
error because "this" in this case refers to the HTML element
the event is handled by
*/

}
document.addEventListener('mousedown', this.MDown, false);
}

var a = new myClass();
When you call myClass() as a constructor, its this keyword refers to
the new object, hence the last line of code creates a new instance of
myClass and assigns it to the global variable 'a', which has properties
a, b and MDown.

However, the value of the this value inside the anonymous function
assigned to a.MDown is evaluated when the function is called and
therefore is determined by how you call the function, not necessarily
by how you declared it.

The this value within a function refers to the object that the function
is called as a method of, or if that can't be determined, the
global/window object.

In Firefox, the this value of a function assigned to an element's
intrinsic event handler by addEventListener and called by an event will
reference the element. In IE, the this value of a function attached
using attachEvent and called by an event will reference the window
object.

If you want MDown() to alert the value of the myClass object's a
property (i.e. a.a) and have it work with both Mozilla and IE event
models, then use the following. It may also help if the values for a
and b are passed to the function rather than being hard coded:

function myClass(aVar, bVar) {
this.a = aVar;
this.b = bVar;

this.MDown = function () {
alert(aVar);
}
if (document.addEventListener){
document.addEventListener('mousedown', this.MDown, false);
} else if (document.attachEvent){
document.attachEvent('onmousedown', this.MDown);
}
}

var a = new myClass('blah 1', 'blah 2');

Note however that this creates a closure between the event handler and
the 'a' object and may lead to "memory leak"[1] problems in IE:

<URL: http://www.jibbering.com/faq/faq_not...res.html#clMem >
1. I have a problem with that term as it is (to me) counter-intuitive.
Memory isn't 'leaking' away, it is being consumed. I guess it refers
to the fact that free memory appears to be leaking. However, I'm not
likely to change jargon that has been entrenched for decades just by
whining about it here. :-)
--
Rob

Sep 21 '06 #2

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

Similar topics

1
by: Bren | last post by:
I'm hoping somebody can help me with the following problem. I'm hoping it's not insurmountable. Here's a basic structure: class EventTarget { public: EventTarget(); virtual ~EventTarget();
18
by: Elder Hyde | last post by:
Hey all, A class of mine needs to tell the outside world when its buffer is not empty. The problem is that C# seems to force you to put the event-raising code in the base class. To illustrate,...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
1
by: Fernando Arámburu | last post by:
Hy everybody, I´m working on an ASP.NET framework and and I need to extend System.Web.HttpApplication. I mean, I need to put some intermediate class between System.Web.HttpApplication and Global...
4
by: Peter van der Veen | last post by:
Hi I have a class with an error event. When i initialize the class from a program the events in the class new sub does not fire. (i don't get them back) When calling anonther sub in the same...
3
by: Carmella | last post by:
Is it possible for a class to create an instance of a private nested class which raises events and then listen for those events? In the example below, when InnerClass sings, OuterClass should say...
2
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
2
by: Macca | last post by:
Hi, I have a windows form project. The form class has a number of methods in it that i want to call from another class I have just created. This class is a finite state machine and is created...
7
by: kenny.deneef | last post by:
Hey all We are working on a project and have alot of forms with textboxes on. Now we want to put some input validation keypress events on those textboxes. We got a parent class where we putted...
4
by: Ty | last post by:
Hello all, I am creating a website in VS 2008 VB.net. On one of my pages I am using the Table control to make a type of calendar a IN/OUT board. The problem I found after I wrote all the code...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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
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...
0
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
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...

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.