【问题标题】:var functionName = function() is not a functionvar functionName = function() 不是函数
【发布时间】:2016-02-02 16:38:56
【问题描述】:

我有一个名为 displayer 的 javascriptclass,由 id 和 1 到 8 之间的无符号整数指定,并且包含一些方法(initialize()、getId() 和 addObject)。
问题是当我在一个对象上创建一个新的显示实例并调用一个方法时,控制台会抛出一个错误,上面写着“TypeError: myDisplayer.initialize is not a function”...

结果是我在上面搜索了太多时间,但我仍然没有看到问题出在哪里。有人有想法吗?

我插入了一个sn-p,方便大家自行判断。

谢谢。

function displayer(a, b) {
  if (typeof a == typeof undefined || typeof b == typeof undefined) {
    err_msg = "The constructor provided for displayer which should be displayer(id, size) does not fit.";
    throw {
      name: "Contructor Error",
      level: "Show Stopper",
      message: err_msg,
      htmlMessage: "<span style='color:red;'>" + err_msg + "</span>",
      toString: function() {
        return this.name + ": " + this.message;
      }
    };
  } else {
    if (typeof a == 'string') {
      var id = a;
      return true;
    } else {
      var id = $(a).prop('id');
      return;
    }

    this.getId = function() {
      return id;
    };

    this.initialize = function() {
      $('#' + id).css('background', '#000000');
      $('#' + id).css('color', '#FFFFFF');
      $('#' + id).css('padding', '5px');
      $('#' + id).css('border-width', '2px');
      $('#' + id).css('border-style', 'solid');
      $('#' + id).css('border-radius', '10px');
      $('#' + id).css('border-color', '#555555');
      switch (b) {
        case 1:
          if (screen.availHeight < 600 || screen.availWidth < 800) {
            $('#' + id).hide();
            err_msg = "The selected screen resolution (800x600) oversizes the actual resolution (" + screen.availWidth + "x" + screen.availHeight + ")";
            throw {
              name: "Fit Error",
              level: "Show Stopper",
              message: err_msg,
              htmlMessage: "<span style='color:red'>" + err_msg + "</span>",
              toString: function() {
                return this.name + ": " + this.message;
              }
            };
            return false;
          } else {
            $('#' + id).css('width', '800px');
            $('#' + id).css('height', '600px');
            return true;
          }
          break;
        case 2:
          if (screen.availHeight < 600 || screen.availWidth < 1024) {
            $('#' + id).hide();
            err_msg = "The selected screen resolution (1024x600) oversizes the actual resolution (" + screen.availWidth + "x" + screen.availHeight + ")";
            throw {
              name: "Fit Error",
              level: "Show Stopper",
              message: err_msg,
              htmlMessage: "<span style='color:red'>" + err_msg + "</span>",
              toString: function() {
                return this.name + ": " + this.message;
              }
            };
            return false;
          } else {
            $('#' + id).css('width', '1024px');
            $('#' + id).css('height', '600px');
            return true;
          }
          break;
        case 3:
          if (screen.availHeight < 768 || screen.availWidth < 1024) {
            $('#' + id).hide();
            err_msg = "The selected screen resolution (1024x768) oversizes the actual resolution (" + screen.availWidth + "x" + screen.availHeight + ")";
            throw {
              name: "Fit Error",
              level: "Show Stopper",
              message: err_msg,
              htmlMessage: "<span style='color:red'>" + err_msg + "</span>",
              toString: function() {
                return this.name + ": " + this.message;
              }
            };
            return false;
          } else {
            $('#' + id).css('width', '1024px');
            $('#' + id).css('height', '768px');
            return true;
          }
          break;
        case 4:
          if (screen.availHeight < 864 || screen.availWidth < 1152) {
            $('#' + id).hide();
            err_msg = "The selected screen resolution (1152x864) oversizes the actual resolution (" + screen.availWidth + "x" + screen.availHeight + ")";
            throw {
              name: "Fit Error",
              level: "Show Stopper",
              message: err_msg,
              htmlMessage: "<span style='color:red'>" + err_msg + "</span>",
              toString: function() {
                return this.name + ": " + this.message;
              }
            };
            return false;
          } else {
            $('#' + id).css('width', '1152px');
            $('#' + id).css('height', '864px');
            return true;
          }
          break;
        case 5:
          if (screen.availHeight < 720 || screen.availWidth < 1280) {
            $('#' + id).hide();
            err_msg = "The selected screen resolution (1280x720) oversizes the actual resolution (" + screen.availWidth + "x" + screen.availHeight + ")";
            throw {
              name: "Fit Error",
              level: "Show Stopper",
              message: err_msg,
              htmlMessage: "<span style='color:red'>" + err_msg + "</span>",
              toString: function() {
                return this.name + ": " + this.message;
              }
            };
            return false;
          } else {
            $('#' + id).css('width', '1280px');
            $('#' + id).css('height', '720px');
            return true;
          }
          break;
        case 6:
          if (screen.availHeight < 768 || screen.availWidth < 1280) {
            $('#' + id).hide();
            err_msg = "The selected screen resolution (1280x768) oversizes the actual resolution (" + screen.availWidth + "x" + screen.availHeight + ")";
            throw {
              name: "Fit Error",
              level: "Show Stopper",
              message: err_msg,
              htmlMessage: "<span style='color:red'>" + err_msg + "</span>",
              toString: function() {
                return this.name + ": " + this.message;
              }
            };
            return false;
          } else {
            $('#' + id).css('width', '1280px');
            $('#' + id).css('height', '768px');
            return true;
          }
          break;
        case 7:
          if (screen.availHeight < 800 || screen.availWidth < 1280) {
            $('#' + id).hide();
            err_msg = "The selected screen resolution (1280x800) oversizes the actual resolution (" + screen.availWidth + "x" + screen.availHeight + ")";
            throw {
              name: "Fit Error",
              level: "Show Stopper",
              message: err_msg,
              htmlMessage: "<span style='color:red'>" + err_msg + "</span>",
              toString: function() {
                return this.name + ": " + this.message;
              }
            };
            return false
          } else {
            $('#' + id).css('width', '1280px');
            $('#' + id).css('height', '800px');
            return true;
          }
          break;
        case 8:
          if (screen.availHeight < 1024 || screen.availWidth < 1280) {
            $('#' + id).hide();
            err_msg = "The selected screen resolution (1280x1024) oversizes the actual resolution (" + screen.availWidth + "x" + screen.availHeight + ")";
            throw {
              name: "Fit Error",
              level: "Show Stopper",
              message: err_msg,
              htmlMessage: "<span style='color:red'>" + err_msg + "</span>",
              toString: function() {
                return this.name + ": " + this.message;
              }
            };
            return false;
          } else {
            $('#' + id).css('width', '1280px');
            $('#' + id).css('height', '1024px');
            return true;
          }
          break;
        default:
          $('#' + id).hide();
          err_msg = "The selected screen resolution (" + b + ") isn't recognized by our system. Resolutions go from 1 to 8.";
          throw {
            name: "Syntax Error",
            level: "Show Stopper",
            message: err_msg,
            htmlMessage: "<span style='color:red>" + err_msg + "<span>",
            toString: function() {
              return this.name + ": " + this.message;
            }
          };
          return false;
          break;
      }
    };
    //c = obj id, d = top pos, e = left pos
    this.addObject = function(c, d, e) {
      if (typeof d == typeof 42 && typeof e == typeof 42) {
        $('#' + id).append($('#' + c));
        posTop = $('#' + id).offset().top + d;
        posLeft = $('#' + id).offset().left + e;
        $('#' + c).offset({
          top: posTop,
          left: posLeft
        });
        return true;
      } else {
        err_msg = "The positions given for the addObject(id, posTop, posLeft) method are not numerical.";
        throw {
          name: "Syntax Error",
          level: "Show Stopper",
          message: err_msg,
          htmlMessage: "<span style='color:red'>" + err_msg + "</span>",
          toString: function() {
            return this.name + ": " + this.message;
          }
        };
        return false;
      }
    };
  }
}

function createDisplayer(id, res) {
  var myDisplayer = new displayer(id, res);
  myDisplayer.initialize();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button onclick="createDisplayer('anId',1);">Create the displayer</button>
<br/>
<div id="anId"></div>

【问题讨论】:

  • 它的 init() 没有初始化
  • @VasimVanzara 怎么样?代码明确指出this.initialize = function() {};
  • myDisplayer.initialize;是你可以像这样访问的对象
  • @VasimVanzara OP 期望 myDisplayer.initialize 成为一个函数。如果你运行代码,你会看到它是undefined
  • 我还在尝试,但没有得到答案

标签: javascript jquery function class


【解决方案1】:

您在处理a 的值时过早返回。

if (typeof a == 'string') {
  var id = a;
  return true; // remove this
} else {
  var id = $(a).prop('id');
  return; // and this
}

了解我是如何发现这一点的可能对您有用。当您认为应该为某事物分配一个给定值但实际上并非如此时,一种常见的做法是将日志记录语句放在预期分配之前或之后。

我在this.initialize = function() { ... 之前放置了一个console.log,然后注意到它从未被记录。这表明该函数必须在该点之前返回。

【讨论】:

  • 否,因为:var myDisplayer = new displayer(id, res); console.log(myDisplayer); prints: displayer {} 并且不是 true 或 undefined
  • @Gavriel Doesn't matter,此时它仍然返回this的值。
  • this 是什么意思?为什么当return true 存在时它会返回this?啊,是因为new displayer(...)吧?
  • @Gavriel 你明白了。
猜你喜欢
  • 2014-11-16
相关资源
最近更新 更多