浏览器图片预加载的时候,用到了Image这个类:var img=new Image();
img.src="http://xxxxx";但是Image类跟我们自己建立的类不太一样,它是一个function没错,可是却没有prototype属性:alert("typeof Image:"+typeof Image+"\n"+"Image.prototype:"+Image.prototype);
/*display
typeof Image:function
Image.prototype:undefined
*/这个虽然不重要,但是当我想给Image的原型建立方法的时候,就出现问题了//Image.prototype.show=function(x){this.src=x;};//这种用法是错的,因为Image没有prototype对象
Image.prototype={
show:function(x){this.src=x;}
};//硬上弓,添加prototype对象
window.onload=function(){
var img=new Image();
document.lastChild.lastChild.appendChild(img);
img.show("http://xxxxxx");//给img添加的show方法,替代img.src属性,测试的时候通不过
alert(img.show);//undefined
};Image既然没有prototype属性,我加了它,可是在用new新建实例后,实例却没有原型中的show方法,很奇怪,不是吗?谁能解释下。
img.src="http://xxxxx";但是Image类跟我们自己建立的类不太一样,它是一个function没错,可是却没有prototype属性:alert("typeof Image:"+typeof Image+"\n"+"Image.prototype:"+Image.prototype);
/*display
typeof Image:function
Image.prototype:undefined
*/这个虽然不重要,但是当我想给Image的原型建立方法的时候,就出现问题了//Image.prototype.show=function(x){this.src=x;};//这种用法是错的,因为Image没有prototype对象
Image.prototype={
show:function(x){this.src=x;}
};//硬上弓,添加prototype对象
window.onload=function(){
var img=new Image();
document.lastChild.lastChild.appendChild(img);
img.show("http://xxxxxx");//给img添加的show方法,替代img.src属性,测试的时候通不过
alert(img.show);//undefined
};Image既然没有prototype属性,我加了它,可是在用new新建实例后,实例却没有原型中的show方法,很奇怪,不是吗?谁能解释下。
解决方案 »
- 告诉我这是什么意思?
- 有没有好的JS混淆器,推荐一点给小弟,谢谢各位
- 【求助】在同一页面,不同表传值问题(JQuery弹框)
- 不懂javascript,请懂的人帮帮忙
- 如何用javascript判断xml的某个节点是否存在
- 将鼠标放在某一段文字或者表格的某一列上面时,会浮出一个小方框出现一些说明文字,请问怎样写?
- 难题:怎样屏蔽window.showModalDialog 弹出窗口右上面的 叉叉 ?
- 关于父窗口控制子窗口元素属性问题,在线等!!!
- 请问怎么判断某个窗口获得了焦点?
- 我的IE的title栏上网时被加上了某站点的名称和url,how to clear it?
- help关于回调函数的问题
- js加载flash 显示进度条
以下IE8 FF3.5下测试通过<script>
Image.prototype.show=function(x){
this.src=x;
};
window.onload=function(){
var img=new Image();
document.lastChild.lastChild.appendChild(img);
img.show("http://avatar.profile.csdn.net/F/2/F/2_songyun18.jpg");
alert(img.show);
};
</script>
要自己写类了