5

I'm looking at someone else's functioning Javascript code. Why are there curly brackets in the parameters when declaring a function? eg:

function createUser({username, password, name, weight}, f) {};

Is this just enforcing and renaming keys that would be in a passed in object? This is in model.js so perhaps it has something to do with validation?

Follow up questions: How can I get this not to error out when I try to compile this on my machine? I get "SyntaxError: Unexpected token {" at the first of these strange brackets.

bplittle
  • 355
  • 2
  • 9
  • 1
    one reason for {} there could be a JSON or object literal, but even then, I think it should be more like `{abc:def, efg:hij, jkl:mln}` or `{abc:"def", efg:"hij", jkl:"mln"}` – barlop Nov 20 '15 at 08:29
  • https://docs.mongodb.org/v3.0/reference/method/db.createUser/ – barlop Nov 20 '15 at 08:34
  • 1
    This does not look like a functioning code. Was it written by someone you work with? Then ask them about this. Is it an open source project? Then please give us a link to it. – Dmytro Shevchenko Nov 20 '15 at 08:36
  • can you post whole file? It seems like not a valid way to declare a function. – Yalamber Nov 20 '15 at 09:07
  • Thanks for the comments. I've gotten in touch with the person who wrote the code for an answer. If it is functioning code I'll post the explanation. – bplittle Nov 20 '15 at 15:31
  • here was the chat with dmytro http://chat.stackoverflow.com/rooms/95661/discussion-between-barlop-and-dmytro-shevchenko – barlop Nov 20 '15 at 23:21

2 Answers2

12

It's an ES6 destructuring assignment.

This syntax declares a function with two parameters.

The values of the username, password, name and weight properties of the object passed as the first argument will be available through the variables username, password, name and weight in the function body.

The second argument will be available through the variable f.

For example:

(function ({a,b}, c) {
  return [a,b,c];
})({a:1, b:2, d:"ignored"}, 3); // [1,2,3]
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • Thanks, this is it! Makes sense. Just needed to run the node app with a --harmony flag to enable it. – bplittle Nov 25 '15 at 06:13
0

Perhaps somebody can answer this better, but one reason for {} there could be a JSON or object literal, but even then, I think it should be more like {abc:def, efg:hij, jkl:mln} (for an object literal - where def,hij e.t.c. are variable names for strings, or string literals) or this for a JSON {"abc":"def", "efg":"hij", "jkl":"mln"}

The mongo documentation for create user https://docs.mongodb.org/v3.0/reference/method/db.createUser/ gives somewhat of an example. Note the property:value pairs separated by commas

use admin
db.createUser(
   {
     user: "appAdmin",
     pwd: "password",
     roles:
       [
         { role: "readWrite", db: "config" },
         "clusterAdmin"
       ]
   }
)

or here, from the same link, but with examples of JSONs and examples of object literals. Note the value pairs, for object literals, and the value pairs for JSONs.

use products

db.createUser( { "user" : "accountAdmin01",
                 "pwd": "cleartext password",
                 "customData" : { employeeId: 12345 },
                 "roles" : [ { role: "clusterAdmin", db: "admin" },
                             { role: "readAnyDatabase", db: "admin" },
                             "readWrite"
                             ] },
               { w: "majority" , wtimeout: 5000 } )

EDIT

Dmytro Shevchenko in my discussion with him, made the important point, that in the formal parameters of a function definition i.e. function abc(....) {} in the ... you would never have a JSON or object literal or any { }. You may pass in an object literal or JSON or function with body, so what you pass in might have { } so they may get passed in as arguments(actual parameters) to a function. But never they'd never be formal parameters. So the code you mention, is wrong on multiple levels.. {abc,def} is invalid as an object literal, invalid as a JSON, and invalid in that even if it was adapted to being a valid object literal or valid JSON, it shouldn't be there!

Here was the chat with Dmytro https://chat.stackoverflow.com/rooms/95661/discussion-between-barlop-and-dmytro-shevchenko

Community
  • 1
  • 1
barlop
  • 12,887
  • 8
  • 80
  • 109
  • 1
    please read up on the difference between JSON and object literals: http://stackoverflow.com/questions/2904131/ — in short, the only way in which you can find JSON in JavaScript is as a part of a string. Also, `{abc:def, efg:hij, jkl:mln}` is invalid, unless `def`, `hij` and `mln` are variable names. Also, `{abc:"def", efg:"hij", jkl:"mln"}` is not valid JSON. – Dmytro Shevchenko Nov 20 '15 at 08:42
  • @DmytroShevchenko You write "{abc:def, efg:hij, jkl:mln} is invalid, unless def, hij and mln are variable names" <--- Yeah they are meant to be variable names. What did you think they were? – barlop Nov 20 '15 at 08:55
  • @DmytroShevchenko In the other case you mention, I see I missed some quotes for the JSON(which makes it an object literal !!). I have corrected it. I am familiar with the difference between a JSON and an object literal, it was a typo where I missed out some quotes, but corrected now. – barlop Nov 20 '15 at 08:58
  • @DmytroShevchenko i'm a little unsure.. I guess a function wouldn't have a JSON in its formal parameter list, but then would a function even ever have an object literal in its formal parameter list? – barlop Nov 20 '15 at 09:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95661/discussion-between-barlop-and-dmytro-shevchenko). – barlop Nov 20 '15 at 09:08
  • Thanks for the comprehensive reply! I've gotten in touch with the person who wrote the code for an answer. Normally when creating the function that would expect the object literal as an argument you'd just declare it with a single parameter name though, correct? `function fName(userObject, f) {};` – bplittle Nov 20 '15 at 15:29
  • @bplittle Yes. Dmytro pointed that out in my discussion with him. You never write a JSON or object literal, in the formal parameters of a function definition. You may have { } if you are passing an object literal or passing a json or passing a function with body.. But where you have `function abc(........)` in that `....` you wouldn't have any `{blah}` you wouldn't have any { } there. – barlop Nov 20 '15 at 23:17