JavaScript中的方法如何使用
方法是分配給物件屬性的函數,當在物件的屬性中定義函數時,它被稱為該物件的方法而不被稱為函數,本篇文章就來給大家介紹關於JavaScript中的方法如何使用中的方法如何使用中的方法如何使用中方法的使用。
屬性是預先設定的特定資訊(值),其中新增了名稱(屬性名稱)。在其屬性中,該函數被特別稱為“方法”。
我們來看具體的範例
在下面的程式中,我們基於交通燈blue,yellow,red建立一個對象,並將其放在一個名為traffic_light的變數中。
<!DOCTYPE html> <html> <head> <meta charset = "utf-8"> <title>JavaScript中的方法如何使用中的方法如何使用中的方法如何使用</title> </head> <body> <script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop" } </script> </body> </html>
我們在這裡新增了一個名為current的屬性。在current中包含交通號誌的顏色。
<script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop", current: " " } </script>
改變交通燈顏色的函數定義為change_light。然後,透過呼叫名為change_light的方法(函數)來變更此current的值。
我們先定義函數change_light
繼續在上面的程式中使用function,讓我們暫時定義函數為change_light。
讓我們考慮下一個訊號呼叫change_light以確定要呼叫的下一個屬性的行為,這取決於當時當前的內容。
使用switch語句將change_light設定為四種模式。
如果current中包含的屬性為blue,則下一個屬性將變更為yellow。
如果current中包含的屬性為yellow,下一個屬性將會變更為red。
如果current中包含的屬性為red,下一個屬性將會變更為blue。
預設是blue。
<script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop", current: " " } function change_light(){ switch(traffic_light.current){ case traffic_light.blue: traffic_light.current = traffic_light.yellow; break; case traffic_light.yellow: traffic_light.current = traffic_light.red; break; case traffic_light.red: traffic_light.current = traffic_light.blue; break; default: traffic_light.current = traffic_light.blue; break } } </script>
接下來,透過在console.log中呼叫current來查看結果
<script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop", current: " " } function change_light(){ switch(traffic_light.current){ case traffic_light.blue: traffic_light.current = traffic_light.yellow; break; case traffic_light.yellow: traffic_light.current = traffic_light.red; break; case traffic_light.red: traffic_light.current = traffic_light.blue; break; default: traffic_light.current = traffic_light.blue; break } } change_light(); console.log(traffic_light.current); </script>
因為預設的屬性設定為blue,所以輸出blue的值為go。
使用console.log重複三次呼叫...
<script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop", current: " " } function change_light(){ switch(traffic_light.current){ case traffic_light.blue: traffic_light.current = traffic_light.yellow; break; case traffic_light.yellow: traffic_light.current = traffic_light.red; break; case traffic_light.red: traffic_light.current = traffic_light.blue; break; default: traffic_light.current = traffic_light.blue; break } } change_light(); console.log(traffic_light.current); change_light(); console.log(traffic_light.current); change_light(); console.log(traffic_light.current); change_light(); console.log(traffic_light.current); </script>
current的變化從blue->yellow->red-> ;blue
值的輸出為go->slow down->stop->go
最後就讓我們來看看讓change_light作為traffic_light的方法的使用
要做的是在current:“”之後設置屬性名稱change_light,並使用以下函數對其進行分隔(比如用“:”分隔它們)。 (此時,連續的函數名稱change_light是重複的,所以可以刪除它)
<script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop", current: " ", change_light:function(){ switch(traffic_light.current){ case traffic_light.blue: traffic_light.current = traffic_light.yellow; break; case traffic_light.yellow: traffic_light.current = traffic_light.red; break; case traffic_light.red: traffic_light.current = traffic_light.blue; break; default: traffic_light.current = traffic_light.blue; break } } } </script>
現在,teaffic_light物件將有一個名為change_light的方法。
我們以同樣的方式在consoe.log中呼叫它。這次重複執行四次。
在呼叫物件中的每個屬性時,可以透過將「.」放入變數名稱後接屬性名稱來呼叫屬性值。因此,當你想要在變數traffic_light中包含的物件中呼叫方法(屬性)時,它變成如下所示。
<script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop", current: " ", change_light:function() { switch(traffic_light.current){ case traffic_light.blue: traffic_light.current = traffic_light.yellow; break; case traffic_light.yellow: traffic_light.current = traffic_light.red; break; case traffic_light.red: traffic_light.current = traffic_light.blue; break; default: traffic_light.current = traffic_light.blue; break } } } traffic_light.change_light(); console.log(traffic_light.current); traffic_light.change_light(); console.log(traffic_light.current); traffic_light.change_light(); console.log(traffic_light.current); traffic_light.change_light(); console.log(traffic_light.current); </script>
運行結果如下:
結果沒有改變,因為函數change_light只是traffic_light的物件的方法。
以上是JavaScript中的方法如何使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

學習JavaScript不難,但有挑戰。 1)理解基礎概念如變量、數據類型、函數等。 2)掌握異步編程,通過事件循環實現。 3)使用DOM操作和Promise處理異步請求。 4)避免常見錯誤,使用調試技巧。 5)優化性能,遵循最佳實踐。

實現視差滾動和元素動畫效果的探討本文將探討如何實現類似資生堂官網(https://www.shiseido.co.jp/sb/wonderland/)中�...

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

深入探討console.log輸出差異的根源本文將分析一段代碼中console.log函數輸出結果的差異,並解釋其背後的原因。 �...
