S
Steve Potter
I am new to JavaScript and having a hard time understanding how to
apply OOP principals to JavaScript.
The problem I am running into is that I am binding an event callback
to an instance method, but when that method tries to access an
instance variable, it returns undefined.
I put together a quick example to demonstrate the issue I am having:
<html>
<head>
</head>
<body>
<a href="#" id="link">Click Here</a></br>
<script type="text/javascript">
function myclass(classattribute){
this.classattrib = classattribute;
this.mymethod = mymethod;
this.mymethod();
this.element = document.getElementById('link');
this.element.addEventListener('click', this.mymethod, false);
}
function mymethod(){
document.write('Class attribute = '+this.classattrib);
}
testclass = myclass('something');
</script>
</body
</html>
Essentually, the first time mymethod is executed (at page load), it
has no problem accessing this.classattrib, when it is called again as
a result of the click event, this.classattrib returns undefined.
I have tried to read some tutorials, I am finding it all very
confusing because it seems that everything in javascript can be done 3
or 4 different ways.
Are there any good tutorials out there for someone trying to learn
javascript coming from a python background?
Thanks,
Steven Potter
apply OOP principals to JavaScript.
The problem I am running into is that I am binding an event callback
to an instance method, but when that method tries to access an
instance variable, it returns undefined.
I put together a quick example to demonstrate the issue I am having:
<html>
<head>
</head>
<body>
<a href="#" id="link">Click Here</a></br>
<script type="text/javascript">
function myclass(classattribute){
this.classattrib = classattribute;
this.mymethod = mymethod;
this.mymethod();
this.element = document.getElementById('link');
this.element.addEventListener('click', this.mymethod, false);
}
function mymethod(){
document.write('Class attribute = '+this.classattrib);
}
testclass = myclass('something');
</script>
</body
</html>
Essentually, the first time mymethod is executed (at page load), it
has no problem accessing this.classattrib, when it is called again as
a result of the click event, this.classattrib returns undefined.
I have tried to read some tutorials, I am finding it all very
confusing because it seems that everything in javascript can be done 3
or 4 different ways.
Are there any good tutorials out there for someone trying to learn
javascript coming from a python background?
Thanks,
Steven Potter