此 HTML 片段创建一个 Object 原型,对其进行实例化,然后尝试使用来自 Event 的该对象的方法,但未成功。
<body>
<button type="button" onclick="TestLogic()">Test Logic</button>
<script>
function onOff() //Object prototype
{
this.state = false;
function setState(newState) {
this.state = newState;
}
}
var inputOne = new onOff(); //Instantiate object prototype
function TestLogic() //buttonClick Event Handler
{
inputOne.setState(true);
// generates Uncaught Type Error undefined is not a function */
document.inputOne.setState(true);
// generates Uncaught Type Error Cannot read property setState of undefined
}
</script>
</body>