onclick Uncaught TypeError:无法读取属性

时间:2017-07-30 02:02:09

标签: javascript html onclick buttonclick

项目设置

  • Webstorm 2017.2
  • 语言javascript,html

项目结构

.
├── Prototype
│   ├── Core
│   │   └── WebBroadcast.js
│   └── index.html
├── README.md

源代码

的index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Web player prototype</title>
    <script src="Core/WebBroadcast.js"></script>
</head>
<body>
    <div>
        <button type="button" onclick="webConnection.connectToServer()">Connect</button>
        <button type="button" onclick="webConnection.disconnectFromServer()">Disconnect</button>
    </div>
    <canvas id="webPlayer" width="640" height="480" style="border:1px solid #000000;"></canvas>

    <script>
        webConnection = WebConnection("ws://192.168.33.30/jpeg/1/jpeg_stream.websock", "admin", "admin", "webPlayer");
    </script>
</body>
</html>

WebBroadcast.js

function WebConnection(address, userName, userPassword) {
    console.log("add: " + address + " name: " + userName + " pwd: " + userPassword)
}

WebConnection.prototype.connectToServer = function () {
    console.log("connect")
}

WebConnection.prototype.disconnectFromServer = function () {
    console.log("disconnect")
}

预期行为

add: ws://192.168.33.30/jpeg/1/jpeg_stream.websock name: admin pwd: admin
connect

实际行为

add: ws://192.168.33.30/jpeg/1/jpeg_stream.websock name: admin pwd: admin
onclick Uncaught TypeError: Cannot read property 'connectToServer' of undefined

这是我第一次尝试javascript项目。 我需要你们的帮助。

1 个答案:

答案 0 :(得分:0)

伙计们我发现了这个问题。

原始

webConnection = WebConnection("ws://192.168.33.30/jpeg/1/jpeg_stream.websock", "admin", "admin", "webPlayer");

固定

webConnection = new WebConnection("ws://192.168.33.30/jpeg/1/jpeg_stream.websock", "admin", "admin", "webPlayer");

new

引起