我有一個疑問。這可能調用一個函數/方法存在於一個類中使用Javascript中的對象原型?如果可能的話,任何人都可以使下面的代碼正確。如何使用Javascript中的對象原型調用函數
(function(){
window.Peer=function(){
var peer=this;
peer.getMessage=function(){
alert("hello!!! This is very good site.")
}
}
})();
<button type="button" id="btn">Click here</button>
<script>
document.getElementById('btn').onclick=function(){
Peer.prototype.getMessage();
}
上面的代碼拋出error.Please給我一些想法來解決這個問題。
你可以測試中控制檯檢查元素 –
可以,因爲它使用'同行運行它(的getMessage())'或'Peer.call(的getMessage())' –
FYI你得到,因爲你的錯誤沒有添加到Peer.prototype的功能 –