【问题标题】:Nodejs: Expecting a function in instanceof check but got undefinedNodejs:在instanceof检查中期望一个函数但未定义
【发布时间】:2015-05-21 16:02:16
【问题描述】:

在使用 node-webkit 时遇到这个奇怪的错误,这是一个可以重新创建它的完整示例:

index.js:

var jQuery = require('jquery');
var Backbone = require('backbone');

(function($){
  var ListView = Backbone.View.extend({
    el: $('body'),
    initialize: function(){
      _.bindAll(this, 'render');    
       this.render();
    },
    render: function(){
      $(this.el).append("<ul> <li>hello world</li> </ul>");
    }
  });
  var listView = new ListView();
})(jQuery);

index.html:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="./css/main.css" />
    </head>
    <body>
    </body>
 <script src="./js/index.js"></script>
</html>

堆栈跟踪:

TypeError: Expecting a function in instanceof check, but got undefined
at _.extend.setElement     (C:\SVN\mapthing\branches\mapthing_js\node_modules\backbone\backbone.js:1046:45)
at _.extend._ensureElement (C:\SVN\mapthing\branches\mapthing_js\node_modules\backbone\backbone.js:1108:14)
at Backbone.View (C:\SVN\mapthing\branches\mapthing_js\node_modules\backbone\backbone.js:1000:10)
at new child (C:\SVN\mapthing\branches\mapthing_js\node_modules\backbone\backbone.js:1566:41)
at file:///C:/SVN/mapthing/branches/mapthing_js/js/index.js:18:18
at file:///C:/SVN/mapthing/branches/mapthing_js/js/index.js:19:3

看不出我的问题出在哪里?

大多数解决方案似乎都指向必须首先要求jquery,但我已经这样做了......

我是一个相对的 n00b,所以我期待我做了一些非常愚蠢的事情......

【问题讨论】:

  • 我认为你没有打电话给instanceof?查看导致问题的代码会很有帮助。如果它发生在后台,您可以发布您的堆栈跟踪吗?
  • @RobM。我已经编辑以提供一个完整的、最小的示例,它给出了相同的行为,以及堆栈跟踪

标签: javascript node.js node-webkit


【解决方案1】:

这是一个 hack,因此请注意这一点,但错误来自主干库中的此方法(来自您的堆栈跟踪的node_modules/backbone/backbone.js:1046):

setElement: function(element, delegate) {
  if (this.$el) this.undelegateEvents();
  this.$el = element instanceof Backbone.$ ? element : Backbone.$(element);
  this.el = this.$el[0];
  if (delegate !== false) this.delegateEvents();
  return this;
},

您很可能可以通过在 Backbone 上显式设置 jQuery 来规避此错误,如下所示:

var jQuery = require('jquery');
var Backbone = require('backbone');
Backbone.$ = jQuery;

这是一个完全未经检验的猜测,但它应该有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2014-02-09
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多