Function Static Varaibles

[Total: 0    Average: 0/5]

This keeps a static variable for a function (it is visible in the scope of the function {and in global also} but it retains it’s value from any previous invocation)

Body Code:

<script type=text/javascript>
var x = 4;
print.x=53;
function print(){
  document.write('print.x='+(++print.x)+'<br>');
}
document.write('x='+x+'&nbsp;&nbsp;print.x='+print.x+'&nbsp;&nbsp;');print()
document.write('x='+x+'&nbsp;&nbsp;print.x='+print.x+'&nbsp;&nbsp;');print()
document.write('x='+x+'&nbsp;&nbsp;print.x='+print.x+'&nbsp;&nbsp;');print()
document.write('x='+x+'&nbsp;&nbsp;print.x='+print.x+'&nbsp;&nbsp;');print()
</script>

Times Viewed: 2

Leave a Reply

Your email address will not be published. Required fields are marked *