![](/img/trans.png)
[英]I get a syntax error when I try to use array map function in Google Scripts. Why?
[英]Why do I get error when I try to alert the length of the passed array to the function?
當我嘗試向函數傳遞傳遞的數組的長度時,為什么會出現錯誤:這是代碼:
<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>
在這一行:
alert(data.length);
我收到此錯誤:
TypeError:數據未定義
為什么會出現此錯誤? 我做錯了什么?
您正在將名稱data
重新綁定到函數參數。 並在沒有任何參數的情況下調用它。 確定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>
要么將data
作為CreatTable
的參數CreatTable
,要么將其刪除,它將引用您定義的data
數組。
您正在調用不帶參數CreatTable()
的函數
而您的函數定義將data
作為參數。
因為data
是全局變量,所以只需將其從函數定義中刪除:
編寫function CreatTable() {
代替
function CreatTable(data) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.