【问题标题】:Javascript Alert box is not showingJavascript 警报框未显示
【发布时间】:2012-03-20 13:39:24
【问题描述】:

我目前正在学习面向对象的 Javascript,但似乎警报不起作用。这里有什么问题?

<

html>
 <head>
  <script>

   function professor(name, myLecture){
        this.name = name;
        this.myLecture = myLecture;
   }

   professor.prototype.display = function(){
        return this.name + " is teaching " + this.myLecture;
   };

   function subjectList(subject){
        this.subject = subject;
   }

   subjectList.prototype.showAll= function(){
            var str = " " ;
            for(var i = 0 ; i<subject.length; i++ )
            str+= this.subject[i].display();
            return str;
   };

   var ListOfSubs = new subjectList([
        new professor("Muy","Obprog")
   ]);

   alert(ListOfSubs.showAll());

  </script>
   <body>
   </body>
 </head>
</html>

【问题讨论】:

  • 您是否在您使用的浏览器中打开了 JavaScript 调试器并查看是否有任何错误?
  • 首先确保您的 javascript 没有错误(ReferenceError: 主题未定义 - 第 17 行)
  • 使用调试器...我得到的错误是“未定义主题”
  • for(var i = 0 ; i
  • @user962206 你可以浏览任何网站...控制台只是执行你的代码。这里应该有更多信息:stackoverflow.com/questions/45965/…

标签: javascript arrays oop prototype


【解决方案1】:

应该是this.subject.length 而不是subject.length

【讨论】:

  • 为什么要使用 this.subject.length?而不是后者?
  • 'subject''showAll' 方法中不“可见”。所以你需要使用'this'
【解决方案2】:

行:

 for(var i = 0 ; i<subject.length; i++ )

错误是“未定义主题”。 从 subject.length = this.subject.length 更改它应该可以解决您的问题。

它应该输出:

Muy is teaching Obprog

【讨论】:

    【解决方案3】:

    你需要this.

       subjectList.prototype.showAll= function(){
                var str = " " ;
                for(var i = 0 ; i< this.subject.length; i++ ) // notice this.subject
                str+= this.subject[i].display();
                return str;
       };
    

    【讨论】:

      【解决方案4】:

      其他人已告知您您的 js 代码有问题的原因。

      我的另一个笔记。

      通常在之前发生异常时不会出现编程警报。

      顺便说一句,你的 body-tag 应该在结束 head-tag 之后

      【讨论】:

        【解决方案5】:

        此代码将在您的脚本标签中运行:

         function professor(name, myLecture){
                this.name = name;
                this.myLecture = myLecture;
           }
        
           professor.prototype.display = function(){
                return this.name + " is teaching " + this.myLecture;
           };
        
           function subjectList(subject){
                this.subject = subject;
           }
        
           subjectList.prototype.showAll= function(){
                    var str = " " ;
                    for(var i = 0 ; i<subject.length; i++ )
                    str+= this.subject[i].display();
                    return str;
           };
        
           var ListOfSubs = new subjectList([
                new professor("Muy","Obprog")
           ]);
        
           alert(ListOfSubs.showAll());
        

        原因:在您的 showAll 函数中,您使用了不存在的主题,但您的原型的对象有一个名为主题的成员。因此,i&lt;this.subject.length 不是 i&lt;subject.length,而是解决您的问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-02-21
          • 2018-06-26
          • 2023-03-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-17
          • 1970-01-01
          相关资源
          最近更新 更多