js 类的问题

来源:百度知道 编辑:UC知道 时间:2024/06/02 19:27:35
<script>

function ret()
{
ret1 = 0;
this.add= function(int)
{
ret1+=int
}
this.gret = function()
{
return ret1;
}
}
function a()
{
ret = new window.ret()
ret.add(2)
alert(ret.gret())
ret=""
}
</script>
<a onclick="a()">asdf</a>

如上代码,为什么第一次执行可以,第二次就不行了呢,对象不支持此操作,怎么解决?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<script>
function ret() {
ret1 = 0;
this.add= function(i) { ret1+=i; }
this.gret = function() { return ret1; }
}
function a(){
r = new ret();
r.add(2);
alert(r.gret());
r=null;
}
</script>
</head>

<body>

<a onclick="a()">asdf</a>
</body>
</html>