V
VJ
I tried to write sample code to get understanding of javascript's
prototypal inheritance (along with the variety of function calling
choices.. )
During the process, I got myself throughly confused.
Can anyone explain following behavior [NOTE: Needs firefox+firebug.
otherwise replace all console.* to alert or equivalent]
here is my code:
<html>
<head>
<script type="text/javascript">
function MyClass(){
if(String(this) == '[object Window]'){
console.debug('Are you sure to use this object in global scope?? Or
you just missed using \'new\' keyword?');
}
this.method = function(){
console.info('in MyClass.method');
}
this.param1='param1';
}
function caller(){
var execClsDirectly = MyClass();
var execClsConstructor = new MyClass();
// execClsDirectly would be null since MyClass does not have any
return statement
if (execClsDirectly) execClsDirectly.param2='added global fn call';
execClsConstructor.param2='added to obj instance';
var clsRef = MyClass;
clsRef.param2='added to class definition';
MyClass.prototype.param2='added to class';
console.log('execClsConstructor param2: '+execClsConstructor.param2);
console.log('outer param2: '+MyClass.param2);
var updatedClsConstructor = new MyClass();
console.log('updatedClsConstructor param2:
'+updatedClsConstructor.param2);
}
</script>
</head>
<body onload="caller();">
</body>
</html>
the output:
Are you sure to use this object in global scope?? Or you just missed
using 'new' keyword?
execClsConstructor param2: added to obj instance
outer param2: added to class definition
updatedClsConstructor param2: added to class
I of course understand the first line of output, but.. confused as to
same param2 added into MyClass gets added into three different levels.
Can anyone elaborate this?
Hope I was able to articulate my confusion in words...
Regards,
VJ
prototypal inheritance (along with the variety of function calling
choices.. )
During the process, I got myself throughly confused.
Can anyone explain following behavior [NOTE: Needs firefox+firebug.
otherwise replace all console.* to alert or equivalent]
here is my code:
<html>
<head>
<script type="text/javascript">
function MyClass(){
if(String(this) == '[object Window]'){
console.debug('Are you sure to use this object in global scope?? Or
you just missed using \'new\' keyword?');
}
this.method = function(){
console.info('in MyClass.method');
}
this.param1='param1';
}
function caller(){
var execClsDirectly = MyClass();
var execClsConstructor = new MyClass();
// execClsDirectly would be null since MyClass does not have any
return statement
if (execClsDirectly) execClsDirectly.param2='added global fn call';
execClsConstructor.param2='added to obj instance';
var clsRef = MyClass;
clsRef.param2='added to class definition';
MyClass.prototype.param2='added to class';
console.log('execClsConstructor param2: '+execClsConstructor.param2);
console.log('outer param2: '+MyClass.param2);
var updatedClsConstructor = new MyClass();
console.log('updatedClsConstructor param2:
'+updatedClsConstructor.param2);
}
</script>
</head>
<body onload="caller();">
</body>
</html>
the output:
Are you sure to use this object in global scope?? Or you just missed
using 'new' keyword?
execClsConstructor param2: added to obj instance
outer param2: added to class definition
updatedClsConstructor param2: added to class
I of course understand the first line of output, but.. confused as to
same param2 added into MyClass gets added into three different levels.
Can anyone elaborate this?
Hope I was able to articulate my confusion in words...
Regards,
VJ