I am trying to get into depth of JavaScript a little more and find it a bit
confusing for person who is used to C#/C++ object oriented approach..
So I need some help in understanding how staff works... Given following
object definition
ZColor = function(num)
{
var a = num + 1;
this.b = num + 2;
this.show = function()
{
alert(a);
alert(this.b);
}
}
I have folowing code that runs when htm page is loaded.
var obj = new ZColor(6);
setInterval(obj .show, 1000);
So it produces 2 alerts. First is "7" and second is "undefined"
So basically show method has access to "a" variable and does not have access
to "b" variable...
I do understand why "alert(this .b)" does not work from timer function. Cause
timer has not idea that i want to run it under "obj" as "this" scope.
But what puzles me is why "alert(a)" works... .
Thanks
George.
confusing for person who is used to C#/C++ object oriented approach..
So I need some help in understanding how staff works... Given following
object definition
ZColor = function(num)
{
var a = num + 1;
this.b = num + 2;
this.show = function()
{
alert(a);
alert(this.b);
}
}
I have folowing code that runs when htm page is loaded.
var obj = new ZColor(6);
setInterval(obj .show, 1000);
So it produces 2 alerts. First is "7" and second is "undefined"
So basically show method has access to "a" variable and does not have access
to "b" variable...
I do understand why "alert(this .b)" does not work from timer function. Cause
timer has not idea that i want to run it under "obj" as "this" scope.
But what puzles me is why "alert(a)" works... .
Thanks
George.
Comment