admin管理员组

文章数量:1362177

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 pile this on my machine? I get "SyntaxError: Unexpected token {" at the first of these strange brackets.

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 pile this on my machine? I get "SyntaxError: Unexpected token {" at the first of these strange brackets.

Share Improve this question asked Nov 20, 2015 at 7:59 bplittlebplittle 3543 silver badges9 bronze badges 6
  • 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 Commented Nov 20, 2015 at 8:29
  • docs.mongodb/v3.0/reference/method/db.createUser – barlop Commented Nov 20, 2015 at 8: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 Commented Nov 20, 2015 at 8:36
  • can you post whole file? It seems like not a valid way to declare a function. – Yalamber Commented Nov 20, 2015 at 9:07
  • Thanks for the ments. 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 Commented Nov 20, 2015 at 15:31
 |  Show 1 more ment

2 Answers 2

Reset to default 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]

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/v3.0/reference/method/db.createUser/ gives somewhat of an example. Note the property:value pairs separated by mas

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./rooms/95661/discussion-between-barlop-and-dmytro-shevchenko

本文标签: