J
Jing You
hi every one,
I have got some confused problem when I try to write some custom object
by javascript.
Look at the example code here:
<BODY>
<script language="jscript">
function Class()
{
this.member = null;
this.event = null;
this.memberFunction = function()
{
alert("memberfunc" + this.member);
this.callEvent();
alert('after call');
}
this.callEvent = function()
{
alert("call:" + this.event);
if (this.event)
{
this.event();
}
}
}
var obj = new Class();
obj.member = "hello";
obj.event = function()
{
alert('event');
}
obj.memberFunction();
</script>
<button id=hello >click</button>
<script>
hello.onclick=obj.memberFunction;
</script>
I try to define a javascript class and define a event handler for it.
There is a member and
a memberFunction and a event in the Class body. Then I created a object
(obj) of Class and
defined a event handler like this:
obj.event = function()
{
alert('event');
}
When I call the function: obj.memberFunction(); the event handler was
fired and alert
a message ('event'). But when I try to use a button to fire the event, it
doesn't work. It seems we
have lost the member function callEvent, it is a undefined value, the same
as this.event member.
Anyone can tell me why?
I have got some confused problem when I try to write some custom object
by javascript.
Look at the example code here:
<BODY>
<script language="jscript">
function Class()
{
this.member = null;
this.event = null;
this.memberFunction = function()
{
alert("memberfunc" + this.member);
this.callEvent();
alert('after call');
}
this.callEvent = function()
{
alert("call:" + this.event);
if (this.event)
{
this.event();
}
}
}
var obj = new Class();
obj.member = "hello";
obj.event = function()
{
alert('event');
}
obj.memberFunction();
</script>
<button id=hello >click</button>
<script>
hello.onclick=obj.memberFunction;
</script>
I try to define a javascript class and define a event handler for it.
There is a member and
a memberFunction and a event in the Class body. Then I created a object
(obj) of Class and
defined a event handler like this:
obj.event = function()
{
alert('event');
}
When I call the function: obj.memberFunction(); the event handler was
fired and alert
a message ('event'). But when I try to use a button to fire the event, it
doesn't work. It seems we
have lost the member function callEvent, it is a undefined value, the same
as this.event member.
Anyone can tell me why?