Home > Article > Web Front-end > Detailed explanation of js method rewriting and overloading techniques
js methods cannot be overloaded. The reason is explained on page 111 of js advanced Programming. To summarize,
js methods can be written like this:
var func1 = function(){ console.log("func1 test"); }123
Such a method is equivalent to a parameter. The overloading of the method means that the method name is the same but the parameters are different, but it is a different method. However, js does not have this because the parameters are different, making them different functions For example:
$(function(){ console.log("------**********开始了************---------"); funTest(100,200,300); })function funTest(num,num1){ console.log("funTest2 satrt") console.log(arguments.length); console.log(arguments[0]); console.log(num); console.log(num1); console.log(num+200) }12345678910111213141516171819
Even if funTest has three parameters and no error is reported, the following function can also be executed, because these two reasons are that the js method is not overloaded. Directly speaking, different parameters will not result in different functions. The js function is equivalent to the later parameters overwriting the previous ones, so js is not overloaded.
JS method rewriting
Method rewriting uses method parametersArray angumentsObject to achieve, I think js objects can be There is no need to rewrite dynamically added methods. Maybe I haven’t encountered the scene yet, so I’ll write it down after using it.
$(function(){ console.log("------**********开始了************---------"); funTest(100,200,300); })function funTest(num,num1){ console.log("funTest2 satrt") console.log(arguments.length); console.log(arguments[0]); console.log(num); console.log(num1); console.log(num+200) }
The above is a detailed explanation of the rewriting and overloading techniques of js methods that I have compiled for you. I hope it will be helpful to you in the future.
Related articles:
Understand the relevant syntax of json
## Detailed explanation on the use of js dynamic introduction
The above is the detailed content of Detailed explanation of js method rewriting and overloading techniques. For more information, please follow other related articles on the PHP Chinese website!