[英]Javascript object Class is not defined Error
我有一个 mainoops.js 文件,其中包含以下代码
var Notifier = function(){
this.socket = io.connect('http://localhost:8080');
this.currentdate = new Date();
}
Notifier.prototype.addCbNotification = function(message){
var datetime = this.currentdate.getDate() + "/" + (this.currentdate.getMonth()+1) + "/" + this.currentdate.getFullYear() + " @ " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds();
var datamessage = " <div class='row'><div class='col-xs-2'><i class='fa fa-envelope'></i></div><div class='col-xs-10'><h5><a href=''>" + message + "</a></h5><small>"+datetime+"</small></div></div>";
$("#callback-btn").addClass('alert-notice');
$( "#callback" ).append( "<li class='list-group-item unread'>"+datamessage+" </li> " );
}
Notifier.prototype.addNotification = function(message){
var datetime = currentdate.getDate() + "/" + (currentdate.getMonth()+1) + "/" + currentdate.getFullYear() + " @ " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds();
var datamessage = " <div class='row'><div class='col-xs-2'><i class='fa fa-envelope'></i></div><div class='col-xs-10'><h5><a href=''>" + message + "</a></h5><small>"+datetime+"</small></div></div>";
$("#notice-btn").addClass('alert-notice');
$( "#notice" ).append( "<li class='list-group-item unread'>"+datamessage+" </li> " );
}
Notifier.prototype.startsocketforcallbback = function(myid) {
// body...
socket.on('callback', function (data) {
var message = data.split("_");
if(myid.toLowerCase().trim() == message[0].toLowerCase().trim()){
this.addCbNotification(message[1]);
}
});
};
Notifier.prototype.startsocketfornotice = function() {
// body...
socket.on('notice', function (data) {
var message = data.split("_");
if(myid.toLowerCase().trim() == message[0].toLowerCase().trim()){
this.addNotification(message[1]);
}
});
};
我在我的 php 文件中调用它如下
<script src="{{asset('/js/mainoops.js')}}"></script>
但是当我尝试在这样的 PHP 页面中实例化它时
<script>
var obj = new Notifier();
obj.startsocketforcallbback('<?php echo Auth::user()->empid; ?>');
obj.startsocketfornotice();
</script>
我正在关注错误
Uncaught ReferenceError: Notifier is not defined
不幸的是, this
是您的回调没有指向您的 Notifier 实例。 尝试使用on(..)
第三个参数:
socket.on('callback', function (data) {
var message = data.split("_");
if(myid.toLowerCase().trim() == message[0].toLowerCase().trim()){
this.addCbNotification(message[1]);
}
},this);
在Notifier.prototype.startsocketforcallbback
和
socket.on('notice', function (data) {
var message = data.split("_");
if(myid.toLowerCase().trim() == message[0].toLowerCase().trim()){
this.addNotification(message[1]);
}
},this);
在Notifier.prototype.startsocketfornotice
使用 jquery :
jQuery(document).ready(function($) {
var obj = new Notifier();
obj.startsocketforcallbback('<?php echo Auth::user()->empid; ?>');
obj.startsocketfornotice();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.