家好,很高興又見面了,我是姜茶的編程筆記,我們一起學習前端相關領域技術,共同進步,也歡迎大家關注、點贊、收藏、轉發,您的支持是我不斷創作的動力
在 JavaScript 中,Array.prototype.pop 是一個常用的方法,用于移除數組的最后一個元素并返回該元素。為了更好地理解其內部機制,今天我們將從零開始,實現一個自定義的 pop 方法,并詳細解析其實現步驟。
首先,我們創建一個 myPop 方法,將其添加到 Array.prototype 上。這個方法將移除數組的最后一個元素,并返回該元素。
Array.prototype.myPop = function() {
// 將傳入的數組轉換為對象
const O = Object(this);
// 獲取數組的長度
const len = O.length >>> 0;
// 如果數組長度為 0,則直接返回 undefined
if (len === 0) {
this.length = 0; // 確保數組長度為 0
return undefined;
}
// 獲取數組的最后一個元素
const element = O[len - 1];
// 刪除數組的最后一個元素
delete O[len - 1];
// 更新數組的長度
this.length = len - 1;
// 返回數組的最后一個元素
return element;
};
讓我們逐步解析這個實現。
我們首先將 this 轉換為一個對象,以便在方法中使用:
const O = Object(this);
使用 >>> 0 操作符將長度轉換為無符號整數,確保其為正整數:
const len = O.length >>> 0;
如果數組長度為 0,則將數組長度設為 0,并返回 undefined:
if (len === 0) {
this.length = 0;
return undefined;
}
我們獲取數組的最后一個元素并將其存儲在 element 變量中:
const element = O[len - 1];
使用 delete 操作符刪除數組的最后一個元素:
delete O[len - 1];
將數組的長度減少 1:
this.length = len - 1;
最后,我們返回存儲在 element 變量中的最后一個元素:
return element;
下面是一個示例,展示如何使用自定義的 myPop 方法:
const arr = [1, 2, 3, 4, 5];
console.log(arr.myPop()); // 輸出 5
console.log(arr); // 輸出 [1, 2, 3, 4]
在這個示例中,我們使用 myPop 方法從 arr 數組中刪除最后一個元素,并返回它。結果輸出為 5,并且 arr 數組變為 [1, 2, 3, 4]。這樣,我們成功地實現了與原生 pop 方法相同的功能。
通過實現自定義的 pop 方法,我們深入理解了 JavaScript 中數組的操作方式。相信你也可以更好地掌握 pop 方法的內部機制,并提升你的 JavaScript 編程技巧。如果你有任何問題或建議,歡迎在評論區留言交流!祝你編程愉快!
HTML 5 也被稱為 Web Applications 1.0。為了實現這個目標,增加了幾個為 Web 頁面提供交互體驗的新元素:
details
datagrid
menu
command
這些元素都可以根據用戶的操作和選擇改變顯示的內容,而不需要從服務器裝載新頁面。
details
details 元素表示在默認情況下可能不顯示的詳細信息??蛇x的 legend 元素可以提供詳細信息的摘要。
details 元素的用途之一是提供腳注和尾注。例如:
The bill of a Craveri's Murrelet is about 10% thinner
than the bill of a Xantus's Murrelet.
<details>
<legend>[Sibley, 2000]</legend>
<p>Sibley, David Allen, The Sibley Guide to Birds,
(New York: Chanticleer Press, 2000) p. 247
</p>
</details>
沒有指定具體的顯示方式。瀏覽器可以選用腳注、尾注和工具提示等方式。
每個 details 元素可以有一個 open 屬性。如果設置了這個屬性,那么詳細信息在最初就顯示出來。如果沒有設置這個屬性,那么會隱藏它們,直到用戶要求顯示它們。無論是哪種情況,用戶都可以通過單擊一個圖標或其他控件來顯示或隱藏詳細信息。
datagrid
datagrid 元素提供一個網格控件。可以用它顯示樹、列表和表格,用戶和腳本可以更新這些界面元素。與之相反,傳統的表格主要用來顯示靜態數據。
datagrid 從它的內容(一個 table、select 或其他 HTML 元素)獲得初始數據。例如,代碼 9 中的 datagrid 包含一張成績表。在這個示例中,datagrid 的數據來自一個 table。更簡單的一維 datagrid 可以從 select 元素獲得數據。如果使用其他 HTML 元素,那么每個子元素成為網格中的一行。
<datagrid>
<table>
<tr><td>Jones</td><td>Allison</td><td>A-</td><td>B </td><td>A</td></tr>
<tr><td>Smith</td><td>Johnny</td><td>A</td><td>C </td><td>A</td></tr>
<tr><td>Willis</td><td>Sydney</td><td>C-</td><td>D</td><td>F</td></tr>
<tr><td>Wilson</td><td>Frank</td><td>B-</td><td>B </td><td>A</td></tr>
</table>
</datagrid>
這個元素與常規表格的區別在于,用戶可以選擇行、列和單元格;把行、列和單元格折疊起來;編輯單元格;刪除行、列和單元格;對網格排序;以及在客戶機瀏覽器中直接進行其他數據操作??梢杂?JavaScript 代碼監視更新。Document Object Model(DOM)中增加了 HTMLDataGridElement 接口以支持這個元素(代碼 10 HTMLDataGridElement)。
interface HTMLDataGridElement : HTMLElement {
attribute DataGridDataProvider data;
readonly attribute DataGridSelection selection;
attribute boolean multiple;
attribute boolean disabled;
void updateEverything();
void updateRowsChanged(in RowSpecification row, in unsigned long count);
void updateRowsInserted(in RowSpecification row, in unsigned long count);
void updateRowsRemoved(in RowSpecification row, in unsigned long count);
void updateRowChanged(in RowSpecification row);
void updateColumnChanged(in unsigned long column);
void updateCellChanged(in RowSpecification row, in unsigned long column);
};
還可以使用 DOM 在網格中動態地裝載數據。也就是說,datagrid 可以不包含那些提供初始數據的子元素。可以用一個 DataGridDataProvider 對象設置它(代碼 11 DataGridDataProvider)。這樣就可以從數據庫、XmlHttpRequest 或者 JavaScript 代碼能夠訪問的任何資源裝載數據。
interface DataGridDataProvider {
void initialize(in HTMLDataGridElement datagrid);
unsigned long getRowCount(in RowSpecification row);
unsigned long getChildAtPosition(in RowSpecification parentRow,
in unsigned long position);
unsigned long getColumnCount();
DOMString getCaptionText(in unsigned long column);
void getCaptionClasses(in unsigned long column, in DOMTokenList classes);
DOMString getRowImage(in RowSpecification row);
HTMLMenuElement getRowMenu(in RowSpecification row);
void getRowClasses(in RowSpecification row, in DOMTokenList classes);
DOMString getCellData(in RowSpecification row, in unsigned long column);
void getCellClasses(in RowSpecification row, in unsigned long column,
in DOMTokenList classes);
void toggleColumnSortState(in unsigned long column);
void setCellCheckedState(in RowSpecification row, in unsigned long column,
in long state);void cycleCell(in RowSpecification row, in unsigned long column);
void editCell(in RowSpecification row, in unsigned long column, in DOMString data);
};
menu 和 command
menu 元素實際上在 HTML 2 中就出現了。在 HTML 4 中廢棄了它,但是 HTML 5 又恢復了它并指定了新的意義。在 HTML 5 中,menu 包含 command 元素,每個 command 元素引發一個操作。例如,代碼 12 HTML 5 菜單 是一個彈出警告框的菜單。
<menu>
<commandlabel="Do 1st Command"/>
<command label="Do 2nd Command"/>
<commandlabel="Do 3rd Command"/>
</menu>
還可以用 checked="checked" 屬性將命令轉換為復選框。通過指定 radiogroup 屬性,可以將復選框轉換為單選按鈕,這個屬性的值是互相排斥的按鈕的組名。
除了簡單的命令列表之外,還可以使用 menu 元素創建工具欄或彈出式上下文菜單,這需要將 type 屬性設置為 toolbar 或 popup。例如,代碼 13. HTML 5 工具欄 顯示一個與 WordPress 等 blog 編輯器相似的工具欄。它使用 icon 屬性鏈接到按鈕的圖片。
<menu type="toolbar">
<commandlabel="strong" icon="bold.gif"/>
<command onclick="insertTag(buttons, 1);"label="em" icon="italic.gif"/>
<command onclick="insertLink(buttons, 2);" label="link" icon="link.gif"/>
<commandlabel="b-quote" icon="blockquote.gif"/>
<command onclick="insertTag(buttons, 4);"label="del" icon="del.gif"/>
<command onclick="insertTag(buttons, 5);"label="ins" icon="insert.gif"/>
<command label="img" icon="image.gif"/>
<commandlabel="ul" icon="bullet.gif"/>
<commandlabel="ol" icon="number.gif"/>
<commandlabel="li" icon="item.gif"/>
<command label="code" icon="code.gif"/>
<command onclick="insertTag(buttons, 11);" label="cite" icon="cite.gif"/>
<command label="abbr" icon="abbr.gif"/>
<command label="acronym" icon="acronym.gif"/>
</menu>
label 屬性提供菜單的標題。例如,代碼14. HTML 5 Edit 菜單 顯示一個 Edit 菜單。
<menu type="popup" label="edit">
<command label="Undo"/>
<command label="Redo"/>
<commandlabel="Cut"/>
<command onclick="copy()" label="Copy"/>
<command onclick="paste()"label="Paste"/>
<command label="Clear"/>
</menu>
菜單可以嵌套在其他菜單中,形成層次化的菜單結構。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。