Sample discussion of parameter passing in javascript functions

  • 2020-03-30 03:37:11
  • OfStack

I believe that everyone who is new to javascript will be confused about the function parameter passing, and the reason is that the syntax is too weird, you define a function

For example,


function test(name,msg){
return 'hello' + name + msg;
}

So what do you do when you call it, you can test('Eric'),test('Eric','welcome to javascript'), or you can even pass it as many arguments as you want, of whatever type you want. However, I will tell you that there is no function overload in javascript, and if you define two functions with the same name, the function you defined first will be overwritten by the function you defined later, meaning that the result you want can only be obtained from the function you defined later.

So, to get back to the point where javascript functions are passed so freely, let's take a look at where the passed arguments are stored. In fact, in its internal implementation, all passed arguments are stored in an array. The function always receives the array, regardless of which parameters the array contains. Now it's easy to understand, we've defined a function that you can pass as many arguments as you want when you call it, and you can put as many as you want into the array, and if you don't, then all of your parameters are 'undefined', which is not syntactically wrong, but semantically difficult.

I LOVE YOU, GUYS!


Related articles: