是什么意思 ({})?

时间:2019-05-09 07:21:27

标签: javascript mongodb ecmascript-6

有时候我使用JavaScript或MongoDB, 我可以看到如下代码:({})

例如以下代码行:

db.users.find({})

但我不知道这到底意味着什么。 你能告诉我那是什么吗?

4 个答案:

答案 0 :(得分:6)

{}是一个空的对象初始值设定项(也称为“对象常量”)。它创建的对象没有其自身的属性。它出现在()中的原因可能是它出现在{{ 1}}将指示一个块的开始。

重新编辑并添加示例:

{

这将创建一个空白对象(db.users.find({}) ),然后使用该对象作为参数调用{}。有关对MongoDB的含义,请参见dvlgs's answer

答案 1 :(得分:3)

关于猫鼬的特殊情况和.find方法

db.users.find({})

表示在集合users中查找所有数据并将其返回(注意db.users.find将返回一个Promise对象,需要解决)

db.users.find({})等同于SELECT * from users WHERE 1(如果是SQL)。

{}表示我们以默认条件(find的第一个参数是条件,即请求的位置)调用函数db.users.find({}) 。默认情况下,参数是您检索所有数据。



在更一般的上下文中,您可以遇到

的多种语法

// #1

// Instantiation of an object
const obj = {};

// Parenthesis can be added here, they have no effect whatsoever
// There are useless
const obj2 = ({});

// Note that this also work
const obj3 = (((({}))));

console.log('objects', obj, obj2, obj3);


// #2

// argument on a function
function f(arg) {
  console.log(arg);
}

// Here we call the function 'f' and set as the first argument of the
// function an object having a key names 'a'
f({
  a: 'arg1',
});

答案 2 :(得分:2)

在什么情况下?

例如,

db.collection.find({})的意思是“列出没有任何过滤器的文档”。

答案 3 :(得分:0)

除了其他答案外,您还经常用括号将对象包装起来,以迫使箭头函数返回对象。

(a,b)=>"abc"         // returns a string
(a,b)=>{}            // does NOT return an object
(a,b)=>{ return {} } // because outer brackets is the function body
(a,b)=>({})          // DOES return an object, because parenthesis 
相关问题