简体   繁体   English

当我尝试向函数传递已传递数组的长度时,为什么会出现错误?

[英]Why do I get error when I try to alert the length of the passed array to the function?

Why do I get error when I try to alert the length of the passed array to the function: Here is code: 当我尝试向函数传递传递的数组的长度时,为什么会出现错误:这是代码:

<div>
    <button onclick = "CreatTable()">click me!</button>
</div>

<script type ="text/javascript">
        var data = ['First Name', 'Last Name', 'Email'];

        function CreatTable(data) {
        alert(data.length);
}
</script>

In this row: 在这一行:

alert(data.length); alert(data.length);

I get this error: 我收到此错误:

TypeError: data is undefined TypeError:数据未定义

Why I get this error? 为什么会出现此错误? What I am doing wrong? 我做错了什么?

You are rebinding name data to a function argument. 您正在将名称data重新绑定到函数参数。 And calling it without any arguments. 并在没有任何参数的情况下调用它。 Sure data is undefined. 确定data未定义。

function CreatTable() { //use scoped variable data.
        alert(data.length);
}
<div>
    <button onclick = "CreatTable()">click me!</button>
</div>

<script type ="text/javascript">
        var data = ['First Name', 'Last Name', 'Email'];

        function CreatTable() {
            alert(data.length);
        }
</script>

Either pass data as an argument to CreatTable , or remove it and it'll refer to the data array you've defined. 要么将data作为CreatTable的参数CreatTable ,要么将其删除,它将引用您定义的data数组。

You're calling the function without an argument CreatTable() 您正在调用不带参数CreatTable()的函数

whereas your function definition takes data as argument. 而您的函数定义将data作为参数。

Because data is a global variable, just remove it from the function definition: 因为data是全局变量,所以只需将其从函数定义中删除:

write function CreatTable() { 编写function CreatTable() {

instead of 代替

function CreatTable(data) {

暂无
暂无

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

相关问题 当我尝试在Google脚本中使用数组映射函数时出现语法错误。 为什么? - I get a syntax error when I try to use array map function in Google Scripts. Why? 反应为什么我在调用作为参数传递的 function 时收到错误“Uncaught TypeError: createTask is not a function”? - REACT Why do I get the error "Uncaught TypeError: createTask is not a function" when calling a function passed as a parameter? 为什么当我下载我的数组并尝试获取它的长度时,它只是吐出数组的字节数 - Why is it when I'm downloading my array and try to get the length of it, it just spits out the number of bytes the array is 当数组作为参数传递并且长度还不存在时,如何利用函数中的数组长度(在JavaScript中)? - How do I utilize the length of an array in a function (in JavaScript) when the array is being passed as a parameter and the length does not exist yet? 为什么当我尝试访问它的数组时,传递给第二个 function 的 object(包含一个数组)会给出一个空数组? - Why does the object (containing an array) passed to second function, give an empty array when I try to access its array? 尝试将中间件功能导出到module.exports对象时,为什么会得到“下一个不是功能”的信息? - Why do I get 'Next is not a function' when I try to export a Middleware function to module.exports object? 尝试进行申请时,为什么会收到一条消息,指出“对象不是函数”? - Why do I get a message saying “Object is not a function” when I try to do an apply? 当我尝试从属性值获取HTML数据时,为什么会出现错误? - Why do I get an error when I try to get HTML data from an attribute value? 当我尝试从“/api/products”获取数据时,为什么会出错? - Why do I get an error when I try to get data from “/api/products”? 谁能告诉我为什么在尝试调用此函数时会出错? - Can anyone tell me why I get a error when i try to call this function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM