我試圖制作一個在 HTML 檔案中寫入一些 div 的函式。一切正常,但我想嘗試從此更改函式宣告:
function writeDiv(divDOMelement, numberOfExcercises, numberOfTasksInEx);
對于這樣的事情
function writeDiv(divDOMelement, {numberOfExcercises:N, numberOfTasksInEx:[v]})
需要明確的是,第一個宣告完美無缺,但是當我嘗試實作第二個宣告并更改函式呼叫時,我收到了一個錯誤。
這是完整的代碼:
let open = false;
let currentlyOpen;
function writeDiv(divDOMelement, {numberOfExcercises:N, numberOfTasksInEx:[v]}) {
for (let i = 0; i < numberOfExcercises.N; i ) {
// ... didnt include the code that prints the divs on the site just to make it easier to read.
for (let j = 0; j < numberOfTasksInEx.v[i]; j ) {
// ... same as the first loop.
}
}
}
$(document).ready(function () {
writeDiv("#mainDiv", {numberOfExcercises:3, numberOfTasksInEx:[5,2,3]});
});
澄清一下,這個 numberOfTasksInEx 應該是一個陣列,陣列的第一個元素告訴我第一個練習會有那么多任務,第二個元素告訴我第二個練習會有那么多任務,依此類推。
我得到的錯誤:
jQuery.Deferred exception: numberOfExcercises is not defined
Uncaught ReferenceError: numberOfExcercises is not defined
uj5u.com熱心網友回復:
您的宣告語法不正確。所有你需要的是:
function writeDiv(divDOMelement, {numberOfExcercises, numberOfTasksInEx})
然后在您的函式中,您將擁有numberOfExercises總計數和numberOfTasksInEx每次練習任務的陣列。
在稱為“分解”的程序中,宣告語法從作為第二個引數傳入函式的單個物件中提取這些屬性。將:在您的版本做一些事情,但你的代碼期望不算什么:它允許從源物件的屬性名(實際引數)被重新命名。這在這里并不是真正必要的,但例如numberOfExercises:N意味著在您的函式中您將簡單地參考N,而不是numberOfExercises.N。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/397088.html
