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.全局的方法,如果访问对象的私有方法和属性
<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.全局的方法,如果访问对象的私有方法和属性
相关文章推荐
- 【学习笔记六】 - js中 创建对象的模式与继承 及 js中实现块级作用域和函数私有变量 《js高程》6-7笔记
- js闭包实现块级作用域和私有变量的访问
- 使用JS闭包控制对象属性访问范围
- OC基础day4-继承,self,super,多态,类对象,SEL类型,description,私有方法与私有变量,三种访问限制
- 对象布局(不通过友元访问私有成员)
- 拷贝构造函数的一个对象访问私有成员的问题
- js对象私有变量公有变量问题
- js之字面量、对象字面量的访问、关键字in的用法
- 服务器端返回的JSON字符串 怎样在 JS中转换成可访问的 JSON对象
- 关于类成员函数中访问同类对象的私有成员
- js访问对象属性两种方式
- javascript面向对象基础——作用域、闭包、模拟私有属性
- js高级编号笔记[新]-访问文档对象
- 关于访问C++类对象中私有成员变量的方法,不考虑用友元
- js 利用image对象实现图片的预加载提高访问速度
- c++中类对象不能访问类的私有成员变量
- js访问xml之 各种对象的属性和方法
- js-return返回多个值,通过对象的属性访问
- js中对象的私有属性和公有属性
- 关于类成员函数中访问同类对象的私有成员