By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,382 Members | 1,637 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,382 IT Pros & Developers. It's quick & easy.

access from inner object

P: n/a
i get "name_ is undefined" error. how should i access ClassA object
from inside this inner function/object?

in Java i would write something like this alert(ClassA.this.name_)

<html>
<head>
<script language="JavaScript">
function ClassA(id) {
this.name_ = "class A";
this.el = document.getElementById(id);

this.el.onmouseover = function() {
this.style.color = "red";
alert(name_);
}
}
</script>
</head>
<body onload="new ClassA('span')">
<span id="span">i'm the <SPAN></span>
</body>
</html>
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> wrote in message news:<3f***********************@news.euronet.nl>.. .
That's because you are assigning the onmouseover event to the element, but
assigning the name to the object.

You should change the order to make this work:

function ClassA(id) {
this.el = document.getElementById(id);
this.el.name_ = "class A";
this.el.onmouseover = function() {
this.style.color = "red";
alert(this.name_);
}
}


i specifically wanted name_ to be a property of ClassA object.
however, i found already a solution:

function ClassA(id) {
this.el = document.getElementById(id);
this.name_ = "class A";

this.el.super_ = this; // give el object a reference to ClassA object
this.el.onmouseover = function() {
this.style.color = "red";
alert(this.super_.name_);
}
}
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.