頁面的結果為 this is a function named 'testFun' with one argument,the argument is undefined
然后按名為 function with one argument test 的按鈕
頁面的結果為 this is a function named 'testFun' with one argument,the argument is test
然后按名為 function with no arguments 的按鈕
頁面的結果為 this is a function named 'testFun2' with no arguments.
然后按名為 function with one argument test 的按鈕
頁面的結果為 this is a function named 'testFun2' with no arguments.
從以上的測試中我們發現我們只是點換了兩個函數的定義順序,結果大不相同。
從上面的測試中我們可以得出結論: 重載的話,只要函數定義在下面就會覆蓋上面的函數定義。
好了,接下來看覆蓋。
按名為 test function share the same name and arguments. 的按鈕
頁面的結果為 this is a function named 'testFun3' second.
測試結果很明顯,結論也是和上面相同的。
最終,我們得出結論:
方法重名,JS會以最后定義的函數作為函數體。當然這不包括JS中的繼承中的覆蓋。
歡迎拍磚
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com