0

I've seen a lot functions in javascript that have 3 parameters , by example second parameter it's a value, and third a function, and when you omit the second parameter , and send the third parameter ( function) like second parameter; there is no error. how it does work ? There are scenarios where second and third parameter are functions and do it that ? What is the architecture behind this?

Examples:

full 3 parameters :

client.query('SELECT $1::int AS number', ['1'], function(err, result) {

2 parameters:

client.query('SELECT  * from accounts', function(err, result) 

3 parameters with null in second parameter:

client.query('SELECT  * from accounts', null, function(err, result) 
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
stackdave
  • 6,655
  • 9
  • 37
  • 56

2 Answers2

1

Before ES2015 there was this way:

function foo(param1, param2, param3){
    if(!param2){
        param2 = standard_value;
    }
    if(!param3){
        param3 = standard_value;
    }
    if(typeof param2 === "function"){
        param3 = param2;
    }
    // do stuff
}

With ES2015 you can use this:

function foo(param1, param2 = standard_value, param3 = standard_value){
    if(typeof param2 === "function"){
        param3 = param2;
    }
    // do stuff
}
Danmoreng
  • 2,367
  • 1
  • 19
  • 32
  • not really, this wouldn't get you the possibility to use param2 as a function, you would have to at least investigate the arguments, or move the arguments around in case the last one is missing – Icepickle Nov 28 '16 at 08:53
  • thanks, the problem is sometimes there is not documentation about javascript npm packages, about which is optional parameter or not, but now i understand better. – stackdave Nov 28 '16 at 09:00
  • thanks @Thalaivar i had to choose like the right answer this, my question was general and i've just wrote one like example, the code is about 'pg' package for postgresql & node – stackdave Nov 28 '16 at 09:07
  • @stackdave: That's fine, hope you understood... dig the source code too... it could sometimes unearth some mysteries... – Thalaivar Nov 28 '16 at 09:08
  • thanks @Thalaivar ; i have a lot of questions about npm packages in javascript with databases; it's not clear which are better, or documentations, maybe you can take a look a question with no reply i did here: http://stackoverflow.com/questions/40810002/couchdb-use-middleware-or-not-wich-for-simple-restful-api-with-express4-nod – stackdave Nov 28 '16 at 09:16
0

So basically your second parameter in the function query is optional.

client.query('SELECT $1::int AS number', ['1'], function(err, result) {});

You can either pass it or no need to pass it, the order function does not get mis-matched in such cases. You check whether the typeof arguments before proceeding further.

So you are basically looking at the connection.query of mysql. If you look at the source-code of mysql, you can look at the createQuery function on how they are handling it.

https://github.com/mysqljs/mysql/blob/2aa8b6c8ea2eb93d4d2afa42920362b707e39aed/lib/Connection.js#L37

Connection.createQuery = function createQuery(sql, values, callback) {
   //look below on how they are handling values
   if (typeof values === 'function') {
       cb = bindToCurrentDomain(values);
    } else if (values !== undefined) {
      options.values = values;
   }
}
Thalaivar
  • 23,282
  • 5
  • 60
  • 71