undefinedfix
Sign in

Some doubts about the value transfer of JavaScript function

Towerss edited in Sun, 17 Jul 2022
(click)="betThis(table, id, name)"

betThis(table, id, name){
    //do something...
}


Above is a click event and function

But suppose I use the same click event in another place today, but it doesn't need to pass three values

It may just need to pass table and name

(click)="betThis(table, '', name)"

Can I only write it like this?

Always think to write a null value strange, there is a way to omit it?

3 Replies
jwood777
commented on Sun, 17 Jul 2022

It can be passed in the form of an object, passing an object as a parameter to a function

betThis({
  table: "",
  id: "",
  name: ""
})

Function internal access to the properties of the object on the line, the problem of parameter changes can be passed like this:

betThis({
  table: "",
  name: ""
})

Secondly, if this is not the case, the uncertain parameter can be put at the end. For example, if the parameter ID is uncertain, it can be as follows:

betThis(table, name, id)

If not, just leave out the last parameter.

Amer
commented on Sun, 17 Jul 2022

Name = name | "any value you want"; or you can put name in the third parameter

harogaston
commented on Sun, 17 Jul 2022

I can't remember whether it's the ES6 or TS specification. The required parameters should be placed before the optional parameters. I think it's better to follow this specification