这两天刚开始学javascript中的prototype自己写了个测试的小程序。但是报错了。不知道错在哪里,有人能指点一下么?<script>
function Ajax(){
this.body=document.getElementById("a");
this.init();
this.getVal();
}
Ajax.prototype=function()
{
init:function(){this.body.innerHTML="test";},
getVal:function (){alert(this.body.innerHTML);}
}
window.onload=function (){new Ajax();}
</script>
function Ajax(){
this.body=document.getElementById("a");
this.init();
this.getVal();
}
Ajax.prototype=function()
{
init:function(){this.body.innerHTML="test";},
getVal:function (){alert(this.body.innerHTML);}
}
window.onload=function (){new Ajax();}
</script>
<script>
function Ajax(){
alert(1);
}
Ajax.prototype=function()
{
alert(2);
}
window.onload=function (){new Ajax();}
</script>
你看这是什么结果
<script>
function Ajax(){
this.body=document.getElementById("a");
this.init=function(){ this.body.innerHTML="test";}
this.getVal=function (){alert(this.body.innerHTML);}
}
Ajax.prototype.go=function()
{
this.init();
this.getVal();
}
window.onload=function (){new Ajax().go();}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Javascript prototype test</title>
<script>
function Ajax(){
this.body=document.getElementById("a");
this.init();
this.getVal();
}
Ajax.prototype = {
init:function(){this.body.innerHTML="test";},
getVal:function (){alert(this.body.innerHTML);}
}window.onload=function (){
new Ajax();
}
</script></head>
<body>
<div id="a">
</div>
</body>
</html>
Ajax.prototype=function()
{
init:function(){this.body.innerHTML="test";},
getVal:function (){alert(this.body.innerHTML);}
}
-》
Ajax.prototype = {
init:function(){this.body.innerHTML="test";},
getVal:function (){alert(this.body.innerHTML);}
}
看看这个会明白些