JS understood from two perspectives Why does not function overloading

Function overloading means within the same scope, there may be a group having the same function name, a list of functions of different parameters (Parameter number, type, order), the set of functions are called overloaded function. Overloaded function is often used to declare a set of functions similar functionality, this reduces the number of function names, to avoid the contamination of the name space, for readability a great advantage.

But if you do not can not be achieved through a number of overloaded methods, can be understood from the following two perspectives in JS.

1. The method of signature

The method signature is the name of the function parameter list added, and the parameter list by name or both can be distinguished function are different functions.

By JS parameter is no way to distinguish between different functions, different functions can only be distinguished by the name of the function, now let's look at the following example.

function add(a, b) {
  return a + b;
}
function add(a, b) {
  return a + b + 1;
}

Above is actually a function, the function add the latter overwrite the previous function, the reason is because JS is weakly typed language, a and b are actually used var declared equivalent to

function add(var a, var b) {
  return a + b;
}

Therefore, in JS only by the function name is not the same to identify a function parameter is of no use.

However, in a strongly typed language is possible, for example, in c language, by defining a function must specify the type of parameter, the value must be passed to the specified type which when passed by value. The same function name, parameter names different functions will be considered to be different functions.

#include <stdio.h>

int add(int a, int b) {
    return a + b;
}
double add(double a, double b) {
    return a + b + 1;
}

int main() {
    printf("第一种声明函数结果 add(1, 3)= %d \n", add(1, 3));
    printf("第二种声明函数结果 add(1.0, 3.0)= %lf \n", add(1.0, 3.0));
}

These two functions are different and can be printed at the results verify.

C operating results Language .png

Therefore, this strongly typed language in C language function can be achieved overloaded, JS weakly this type of language is not possible.

2. The function pointer

In a further appreciated that the angle may be imagined as a pointer to the function name, the function name is stored in the address stored in the function body. It is illustrated by the code.

function add(a, b) {
  return a + b;
}
function add(a, b) {
  return a + b + 1.0;
}

Both of the above statement is equivalent to the way

var add = function (a, b) {
  return a + b;
}
add = function (a, b ) {
  return a + b + 1.0;
}

The final point to add is actually the second function, the first function is covered, so the call is the final call to add a second function, overloaded and can not implement the function.

console.log(add(1, 3)) // 5

Finished, if inappropriate place, please correct me oh.

The second function is the inability to achieve function overloading.

console.log(add(1, 3)) // 5

Finished, if inappropriate place, please correct me oh.

Guess you like

Origin www.cnblogs.com/zhangguicheng/p/12155512.html