It (this) points to the instance, but depending upon execution time and/or scope, a variable may need to be set ahead of time to the instance. However, in its simplest form:
Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script>
function myfunction(string){
this.string = string;
this.blab();
}
myfunction.prototype = {
blab: function(){
alert(this.string);
}
}
</script>
</head>
<body>
<script>
new myfunction('Hello World!');
</script>
</body>
</html>
Using this.property (this.string) is fine.
Adding a this.method within the prototype object:
Code:
function myfunction(string){
this.string = string;
this.preblab();
}
myfunction.prototype = {
preblab: function(){
this.string = this.string.toUpperCase();
this.blab();
},
blab: function(){
alert(this.string);
}
}
But like I say, if the timing (or anything else for that matter) brings it out of scope, a variable must be assigned ahead of time:
Code:
function myfunction(string){
this.string = string;
this.preblab();
}
myfunction.prototype = {
preblab: function(){
this.string = this.string.toUpperCase();
this.blab();
},
blab: function(){
var instance = this;
setTimeout(function(){alert(instance.string);}, 3000);
}
}
Bookmarks