JavaScript自定义类的prototype怎么使用?
<html>
<head>
<title></title>
<script language="javascript" type="text/javascript">
function Marquee(container,loop1){
this.container = document.getElementById(container);
this.loop2 = "";
this.loop1 = document.getElementById(loop1);
this.start=function(){alert(this.loop2);},
this.stop=function(){alert('stop');}
}
var marquee = new Marquee("container","loop1");
marquee.prototype.loop2="ssssss";
</script>
</head>
<body>
<DIV id="container" onclick="marquee.start()">ssssss</div>
<div id="loop1" onclick="marquee.stop()">fffffff</div>
</body>
</html>
为什么在下面点击sssss的时候loop2的值没有变化呢??????????
------解决方案--------------------prototype 是对象类的原型进行扩展,而不是你 new 出来的实例。
function Marquee(container,loop1)
{
this.container = document.getElementById(container);
this.loop1 = document.getElementById(loop1);
this.start=function(){alert(this.loop2);},
this.stop=function(){alert( 'stop ');}
}
Marquee.prototype.loop2="ssssss";
var marquee = new Marquee("container","loop1");