简体   繁体   English

我一直收到这个错误“Uncaught TypeError:name.push不是一个函数 <anonymous> :10:7”

[英]i keep getting this error “Uncaught TypeError: name.push is not a function at <anonymous>:10:7”

 var name = []; var input = prompt("what u want?"); while (input !== "quit"){ if (input === "list"){ console.log(name); } else if(input === "new"){ var hold = prompt("Enter"); name.push(hold); } input = prompt("What u want?"); } console.log("u quit"); 
So here is the situation, i am trying to run this TODO_APP and i think i did everything right, but i am keep on getting error at push section "name.push is not a function" tell me where i am doing wrong.. 所以这是情况,我想尝试运行这个TODO_APP ,我想我做的一切都正确,但我继续在推送部分 “name.push不是一个功能”得到错误告诉我我在哪里做错了..

You can't change the type of name as it is already defined globally . 您无法更改全局定义 name类型 Use a different variable - name1 . 使用其他变量 - name1

 var name1 = []; var input = prompt("what u want?"); while (input !== "quit") { if (input === "list") { console.log(name1); } else if (input === "new") { var hold = prompt("Enter"); name1.push(hold); } input = prompt("What u want?"); } console.log("u quit"); 

Issue in your code is that, any variable that is defined outside any function is added in global scope( window ). 代码中的问题是,在任何函数之外定义的任何变量都会添加到全局范围( window )中。 Now window already has a property called name and you cannot override it. 现在window已经有一个名为name的属性,你不能覆盖它。 Hence it throws error. 因此它会抛出错误。

Solution: 解:

  • Change the variable name 更改变量名称
  • Wrap code in a IIFE so you do not bleed to global scope. 在IIFE中包装代码,这样您就不会流失到全局范围。 Following is a sample of it. 以下是它的一个例子。

 (function() { var name = []; var input = prompt("what u want?"); while (input !== "quit") { if (input === "list") { console.log(name); } else if (input === "new") { var hold = prompt("Enter"); name.push(hold); } input = prompt("What u want?"); } console.log("u quit", name); })() 


Note: its a bad practice to bleed to global scope. 注意:向全局范围流血是一种不好的做法。

References: 参考文献:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'TypeError: name.push is not a function' 同时将字符串推入空数组 - 'TypeError: name.push is not a function' while pushing a string into an empty array 为什么我会收到这个错误:Uncaught TypeError: this.createLink is not a function at new Link at<anonymous> 1:9? - Why am I getting this error: Uncaught TypeError: this.createLink is not a function at new Link at <anonymous>1:9? 尝试使用语义 ui 时,我不断收到此错误 Uncaught TypeError: $(...).modal is not a function - When trying to use semantic ui I keep getting this error Uncaught TypeError: $(...).modal is not a function 错误出现为“Uncaught TypeError: notesObj.push is not a function at HTMLButtonElement。<anonymous> “。请任何人建议做什么</anonymous> - Error comes as "Uncaught TypeError: notesObj.push is not a function at HTMLButtonElement.<anonymous>" . Please anyone suggest what to do 我不断收到错误“ main.js:19 Uncaught TypeError:firebase.database不是函数” - I keep getting the error “main.js:19 Uncaught TypeError: firebase.database is not a function” 我收到此错误Uncaught TypeError:this.getElements不是一个函数 - I'm getting this error Uncaught TypeError: this.getElements is not a function 即使我的代码看起来很好,我仍然会收到“未捕获的TypeError:字符串不是函数”错误。任何人都可以看看并解释一下吗? - I keep getting a “Uncaught TypeError: string is not a function” error, even though my code seems to be fine. Can anyone have a look and explain? 未捕获的TypeError:$ .get不是函数 <anonymous> :1:3 - Uncaught TypeError: $.get is not a function at <anonymous>:1:3 未捕获的TypeError:data.push不是函数。 我怎样才能解决这个推送错误? - Uncaught TypeError: data.push is not a function. How can I get around this push error? 获取错误未捕获TypeError:string不是函数 - Getting error Uncaught TypeError: string is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM