Javascript小部件:未捕获类型错误:对象#<;附录1>;没有方法';添加';

Javascript小部件:未捕获类型错误:对象#<;附录1>;没有方法';添加';,javascript,Javascript,下面是一些解释错误的示例代码: HTML: <!DOCTYPE HTML> <html> <head> <title>Test</title> </head> <body> <h1>Widget Test</h1> <script type="text/javascript"> (function() { var script = documen

下面是一些解释错误的示例代码:

HTML:
<!DOCTYPE HTML>
<html>
<head>
  <title>Test</title>
</head>
<body>
  <h1>Widget Test</h1>
  <script type="text/javascript">
    (function() {
      var script = document.createElement('script'); script.type = 'text/javascript';           
      script.async = true;
      script.src = 'http://localhost/job/widget.js';
      var s = document.getElementsByTagName('head')[0].appendChild(script);
    })();
  </script>
  <div id="list" data-cnumber="21"></div>
</body>
</html>


widget.js:

(function() {
  var a = new app();
  var a1 = new app1();

  function app() {
    this.a;
    this.b;
  }
  app.prototype.add = function () {

  };

  function app1() {
    this.a;
    this.b;
    this.c;
    this.add();
  }
  app1.prototype.add = function () {

  };
})();
HTML:
试验
小部件测试
(功能(){
var script=document.createElement('script');script.type='text/javascript';
script.async=true;
script.src=http://localhost/job/widget.js';
var s=document.getElementsByTagName('head')[0].appendChild(脚本);
})();
widget.js:
(功能(){
var a=新应用程序();
var a1=新的app1();
函数app(){
这个,a,;
这是b;
}
app.prototype.add=函数(){
};
函数app1(){
这个,a,;
这是b;
这是c;
这个。添加();
}
app1.prototype.add=函数(){
};
})();
  • 这就是错误——“uncaughttypeerror:Object”没有方法“add”
  • 在对这两个对象执行console.log时,我可以看到它们的属性,但由于某些原因,不能看到它们的方法

  • 怎么了?

    您在调用构造函数之后创建原型成员,因此在构造函数调用期间它们不可用,必须在调用构造函数之前创建原型成员

    (function() {
      app.prototype.add = function () {
    
      };
      app1.prototype.add = function () {
    
      };
    
      var a = new app();
      var a1 = new app1();
    
      function app() {
        this.a;
        this.b;
      }
    
    
      function app1() {
        this.a;
        this.b;
        this.c;
        this.add();
      }
    
    })();
    

    在将“add”函数添加到prototype之前调用“app1”的构造函数。尝试在将“add”函数添加到prototype后调用构造函数