您的位置:首页 > Web前端 > JavaScript

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

2008-10-24 01:13 127 查看
先看一段代码
<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.全局的方法,如果访问对象的私有方法和属性
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: