【问题标题】:How to print to the screen - "console.log is not a function"如何打印到屏幕 - “console.log 不是函数”
【发布时间】:2012-12-13 12:34:29
【问题描述】:

我尝试了很多方法...我做错了什么???我决心学习并真正理解这一点。

//On line 2, declare a variable myName and give it your name.
var myName = "Jeanne";
//On line 4, use console.log to print out the myName variable.
console.log ("Jeanne");
//On line 7, change the value of myName to be just the first 2 letters of your name.
myName.substring(0, 2);
//On line 9, use console.log to print out the myName variable;
console.log("Jeanne");

【问题讨论】:

标签: javascript


【解决方案1】:

大概你给console.log分配了其他东西,不是函数console.log = 5 console.log()

// 显然不再指代函数重新启动您的环境。 IE。刷新页面

【讨论】:

    【解决方案2】:

    我遇到了console.log is not a function 错误,并花了令人沮丧的 15 分钟调试它。原来我有一个名为console的局部变量!使用 window.console.log("...") 成功了。

    【讨论】:

      【解决方案3】:

      为了让 console.log 打印出你的 var 值,你应该在 console.log

      的括号之间包含 variable

      如下例所示;

      //在第2行,声明一个变量myName并给它你的名字。 var myName = "珍妮";

      //在第4行,使用console.log打印出myName变量。 console.log (myName);

      【讨论】:

      • 问的不是这个问题。
      【解决方案4】:

      感谢大家的帮助!我非常感谢您能够有空且知识渊博地回答问题。我确实想通了。显然问题出在子字符串上。我已经完整地输入了单词 substring。我猜它不喜欢那样,只需要缩写的 substr。

      //On line 2, declare a variable myName and give it your name.
      var myName = "Jeanne";
      //On line 4, use console.log to print out the myName variable.
      console.log (myName);
      //On line 7, change the value of myName to be just the first 2 
      //letters of your name.
      myName = myName.substr(0,2);
      //On line 9, use console.log to print out the myName variable;
      console.log (myName);
      

      【讨论】:

        【解决方案5】:

        您的错误可能是您每次都在屏幕上看到“Jeanne”。

        这是因为您打印的是常量,而不是变量。试试用这个打印:

        myName = myName.substring(0, 2);
        console.log (myName);
        

        正如其他答案中所述 - 确保您使用的是支持 console.log 的浏览器

        【讨论】:

        • @tooheymomster 好吧,如果在帖子中包含此类信息,这将很容易解决。我怀疑这是因为正在使用旧版本的 IE ..
        【解决方案6】:

        确保您使用的浏览器支持console.log(Firefox with Firebug, Google Chrome) 如果您的浏览器出现问题,请尝试在 jsfiddle 中运行您的代码

        编辑

        我尝试在 jsfiddle 中运行您的代码,并且在我的浏览器(Chrome 和 IE9)中,它们都可以完美运行。

        根据我读到的评论,您正在使用 firefox。如果是这种情况,请确保您安装了 firebug 插件。你可以得到它here

        【讨论】:

          【解决方案7】:

          你需要assign将子串的结果返回给变量才能得到子串的结果。使用该变量打印substring result

          Live Demo

          myName = myName.substring(0, 2);
          console.log(myName );
          

          【讨论】:

            【解决方案8】:

            字符串是不可变的,因此您应该将结果分配给一个新变量。

            【讨论】:

              【解决方案9】:

              除其他事项外,请确保您使用的浏览器支持“console.log”。这意味着 Chrome 或安装了 Firebug 插件的 FireFox,或 打开了开发人员工具的 Internet Explorer。然后当然打开开发者工具。对于 Chrome,它是 ctrl-shift-I;其他一切都是 F12。

              【讨论】:

              • 在这种情况下,请确保安装 Firebug。
              【解决方案10】:

              您所做的一切都是正确的,只需记录已处理的变量而不是原始名称,完成!

              //On line 2, declare a variable myName and give it your name.
              var myName = "Jeanne";
              //On line 4, use console.log to print out the myName variable.
              console.log (myName );
              //On line 7, change the value of myName to be just the first 2 letters of your name.
              myName=myName.substring(0, 2);
              //On line 9, use console.log to print out the myName variable;
              console.log(myName );
              

              【讨论】:

                猜你喜欢
                • 2013-05-28
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多