【问题标题】:Javascript object Class is not defined ErrorJavascript对象类未定义错误
【发布时间】:2016-03-25 14:01:15
【问题描述】:

我有一个包含以下代码的 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

【问题讨论】:

  • 请给出拒绝投票的理由,谢谢

标签: javascript php function-prototypes


【解决方案1】:

不幸的是,this 是您的回调没有指向您的通知程序实例。尝试使用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

【讨论】:

  • 我没有得到它应该在套接字中更新this 吗?我试过了,但它不起作用
  • 在 javascript 中,this 不像在 Java 或任何其他语言中那样指向您的对象实例。 this 表示(非常类比)当前方法/函数的执行上下文。因此,当套接字调用您的回调函数时,它可能会将当前上下文设置为 null 或 window.但是,恐怕这个错误(Uncaught ReferenceError: Notifier)意味着该页面没有加载您的mainoops.js,因此它对您的通知程序一无所知。尝试在 Chrome 中使用 Developer tools 或在 Firefox 中使用“开发者”菜单来了解为什么浏览器没有加载您的 js。
【解决方案2】:

使用 jquery:

jQuery(document).ready(function($) {
    var obj = new Notifier();
        obj.startsocketforcallbback('<?php echo Auth::user()->empid; ?>');
        obj.startsocketfornotice();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 2021-07-31
    • 2021-08-10
    • 1970-01-01
    相关资源
    最近更新 更多