先看一段代码
代码中反映了两个问题
1.js变量的作用域
2.全局的方法,如果访问对象的私有方法和属性
- <style>
- div { font-size:12px; }
- </style>
- <div id="mq" style="width:100%;height:70px;overflow:hidden" onmouseover="obj.iScrollAmount=0" onmouseout="obj.iScrollAmount=1">
- 1<br/>2<br/>3<br/>4<br/>5<br/></div>
- <br><br>
- <script>
- function srollDiv(divid,objname)
- {
- this.Name=objname;
- this.id=srollDiv.names.length;
- srollDiv.names[this.id]=this;
- this.oMarquee=document.getElementById(divid);
- this.iLineHeight=42;
- this.iLineCount=7;
- this.iScrollAmount=1;
- }
- srollDiv.names=new Array();
- srollDiv.prototype.innerRun =function()
- {
- var self=this;
- this.oMarquee.scrollTop += this.iScrollAmount;
- if ( this.oMarquee.scrollTop == this.iLineCount * this.iLineHeight )
- this.oMarquee.scrollTop = 0;
- if ( this.oMarquee.scrollTop % this.iLineHeight == 0 ) {
- window.setTimeout( function(){self.innerRun();}, 2000 );
- } else {
- window.setTimeout( function(){self.innerRun();}, 50 );
- }
- }
- srollDiv.prototype.run=function()
- {
- var self=this;
- this.oMarquee.innerHTML += this.oMarquee.innerHTML;
- setTimeout( function(){self.innerRun();}, 2000 );
- }
- var obj =new srollDiv('mq','obj');
- obj.run();
- function test()
- {
- this.aa =1;
- this.bb = function test2(){
- alert(this.aa)
- }
- eval('window.aa='+this.bb);
- alert(this.aa)
- }
- function test1()//js以全局变量最大,如果发生全局变量和类属性重名,则如果在类中调this.属性,则该属性为全局变量.
- {
- this.aa =1;
- this.dd=2
- this.cc=function(){
- alert(this.dd)
- }
- this.bb = function(){
- alert(this.aa)
- }
- cc()
- eval('window.cc='+this.bb);//同名则会将变量最大化
- cc()
- }
- test1();
- cc();
- </script>
1.js变量的作用域
2.全局的方法,如果访问对象的私有方法和属性