我是基于 OOP 的 JavaScript 的新手。出于测试目的,我创建了以下代码段。我已经创建了四个按钮,点击每个按钮都会有不同的功能。当我第一次单击任何按钮时,它会给出预测的结果。在第二次单击任何按钮时,它给了我以下错误:
classFunction = new classFunction();
我使用的代码如下:
<html>
<head>
<script type="text/javascript">
var classFunction;
function classFunction() {
this.publicVariable = 'Public Varibale';
var privateVariable = 'Private Variable';
this.publicMethod = function () {
alert('Public Method');
}
function privateMethod() {
alert('private Method');
}
return this;
}
function privateVariable() {
classFunction = new classFunction();
alert(classFunction.privateVariable);
}
function publicvariable() {
classFunction = new classFunction();
alert(classFunction.publicVariable);
}
function privateFunction() {
classFunction = new classFunction();
classFunction.privateMethod();
}
function publicFunction() {
classFunction = new classFunction();
classFunction.publicMethod();
}
</script>
</head>
<body>
<input type="button" value="Private Variable" onclick="privateVariable()" />
<input type="button" value="Public Variable" onclick="publicvariable()" />
<input type="button" value="Private Function" onclick="privateFunction()" />
<input type="button" value="Public Function" onclick="publicFunction()" />
</body>
</html>
新的操作员第二次不工作有什么原因吗?