access from inner object

M

marius

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>
 
M

marius

Janwillem Borleffs said:
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_);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top