js的作用域和访问对象私有域

先看一段代码
  1. <style>
  2.  div { font-size:12px; }
  3. </style>
  4. <div id="mq" style="width:100%;height:70px;overflow:hidden" onmouseover="obj.iScrollAmount=0" onmouseout="obj.iScrollAmount=1">
  5. 1<br/>2<br/>3<br/>4<br/>5<br/></div>
  6. <br><br>
  7. <script>
  8. function srollDiv(divid,objname)
  9. {
  10.     this.Name=objname;
  11.     this.id=srollDiv.names.length;
  12.     srollDiv.names[this.id]=this;
  13.     this.oMarquee=document.getElementById(divid);
  14.     this.iLineHeight=42;
  15.     this.iLineCount=7;
  16.     this.iScrollAmount=1;
  17. }
  18. srollDiv.names=new Array();
  19. srollDiv.prototype.innerRun =function()
  20. {
  21.     var self=this;
  22.     this.oMarquee.scrollTop += this.iScrollAmount;
  23.     if ( this.oMarquee.scrollTop == this.iLineCount * this.iLineHeight )
  24.         this.oMarquee.scrollTop = 0;
  25.     if ( this.oMarquee.scrollTop % this.iLineHeight == 0 ) {
  26.         window.setTimeout(  function(){self.innerRun();}, 2000 );
  27.     } else {
  28.         window.setTimeout( function(){self.innerRun();}, 50 );
  29.     }
  30. }
  31. srollDiv.prototype.run=function()
  32. {
  33.     var self=this;
  34.     this.oMarquee.innerHTML += this.oMarquee.innerHTML;
  35.   setTimeout( function(){self.innerRun();}, 2000 );
  36. }
  37. var obj =new srollDiv('mq','obj');
  38. obj.run();
  39. function test()
  40. {
  41.     this.aa =1;
  42.     this.bb = function test2(){
  43.         alert(this.aa)
  44.     }
  45.     eval('window.aa='+this.bb);
  46.     alert(this.aa)
  47. }
  48. function test1()//js以全局变量最大,如果发生全局变量和类属性重名,则如果在类中调this.属性,则该属性为全局变量.
  49. {
  50.     this.aa =1;
  51.     this.dd=2
  52.     this.cc=function(){
  53.         alert(this.dd)
  54.     }
  55.     this.bb = function(){
  56.         alert(this.aa)
  57.     }
  58.     cc()
  59.     eval('window.cc='+this.bb);//同名则会将变量最大化
  60.     cc()
  61. }
  62. test1();
  63. cc();
  64. </script>
代码中反映了两个问题
1.js变量的作用域
2.全局的方法,如果访问对象的私有方法和属性
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值