我迫切希望有人能给我一些简明的信息,告诉我什么时候应该在 JS ES6 中使用哪些括号以及为什么使用这些括号。我知道基础知识,但是当我们开始谈论箭头语法时,我只是把它弄丢了,然后就看不出我们为什么要用括号括起来等等......我觉得为了真正理解为什么我们以我们的方式布置事物我是否需要首先了解 {} 和 () 的所有用例。
例如。我真的很难找出这样的语法:
const func = (obj) => {
console.log(obj.a)
}
func({a: "blue"})
这是我在这里苦苦挣扎的 func({a: "blue"}) 部分。
这是另一个例子:
makeSound({
a: "bark",
b: 2,
c: "hiss"
})
function makeSound(options)
console.log("the" + options.a + "was a " + options.c)
我不知道该怎么做。我们在 makeSound 的顶部做什么?我可以看到我们正在制作一个对象,但是为什么我们不直接将它声明为具有标准 let makeSound = {} 的变量。我们实际上在这里做什么? makeSound 是什么都没有,直到我们把它变成一个函数,再往下看?
最佳答案
It's the func({a: "blue"}) part I'm struggling with here.
{a: "blue"}
是一个对象字面量。结果对象作为参数传递给 func(...)
.I can see we're making an object but then why aren't we just declaring it as a variable with standard let makeSound = {}.
因为它只需要一次。
let details = {
a: "bark",
b: 2,
c: "hiss"
};
makeSound(details);
... 会给出相同的结果,但现在你得到了
details
您不再需要的变量。Is makeSound nothing untill we make it into a function further down the code?
函数声明是 吊装所以即使声明稍后出现,它也是一个函数。
关于javascript - ES6 JavaScript 中的括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54768049/