Home > Web Front-end > JS Tutorial > Detailed explanation of js method rewriting and overloading techniques

Detailed explanation of js method rewriting and overloading techniques

亚连
Release: 2018-05-17 10:06:54
Original
4242 people have browsed it

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
Copy after login

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
Copy after login

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)
}
Copy after login

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:

How to use JSON

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template