未定义Javascript对象Class错误


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();
});