JavaScript 函数参数内的大括号

发布于 2024-10-02 06:31:26 字数 164 浏览 0 评论 0原文

JavaScript 函数参数周围的大括号有什么作用?

var port = chrome.extension.connect({name: "testing"});
port.postMessage({found: (count != undefined)});

What do the curly braces surrounding JavaScript arguments for functions do?

var port = chrome.extension.connect({name: "testing"});
port.postMessage({found: (count != undefined)});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清醇 2024-10-09 06:31:26

自提出此问题以来,出现了第二个可能的答案Javascript ES6 引入了解构赋值

var x = function({ foo }) {
   console.log(foo)
}

var y = {
  bar: "hello",
  foo: "Good bye"
}

x(y)


Result: "Good bye"

A second possible answer has arisen since this question was asked. Javascript ES6 introduced Destructuring Assignment.

var x = function({ foo }) {
   console.log(foo)
}

var y = {
  bar: "hello",
  foo: "Good bye"
}

x(y)


Result: "Good bye"
泪冰清 2024-10-09 06:31:26

大括号表示对象文字。它是一种发送键/值对数据的方式。

所以这样:

var obj = {name: "testing"};

像这样使用来访问数据。

obj.name; // gives you "testing"

您可以为对象提供多个以逗号分隔的键/值对,只要键是唯一的。

var obj = {name: "testing",
           another: "some other value",
           "a-key": "needed quotes because of the hyphen"
          };

您还可以使用方括号来访问对象的属性。

如果是“a-key”,则需要这样做。

obj["a-key"] // gives you "needed quotes because of the hyphen"

使用方括号,您可以使用存储在变量中的属性名称来访问值。

var some_variable = "name";

obj[ some_variable ] // gives you "testing"

The curly braces denote an object literal. It is a way of sending key/value pairs of data.

So this:

var obj = {name: "testing"};

Is used like this to access the data.

obj.name; // gives you "testing"

You can give the object several comma separated key/value pairs, as long as the keys are unique.

var obj = {name: "testing",
           another: "some other value",
           "a-key": "needed quotes because of the hyphen"
          };

You can also use square brackets to access the properties of the object.

This would be required in the case of the "a-key".

obj["a-key"] // gives you "needed quotes because of the hyphen"

Using the square brackets, you can access a value using a property name stored in a variable.

var some_variable = "name";

obj[ some_variable ] // gives you "testing"
转身泪倾城 2024-10-09 06:31:26

JavaScript 中的大括号用作创建对象的简写。例如:

// Create an object with a key "name" initialized to the value "testing"
var test = { name : "testing" };
alert(test.name); // alerts "testing"

查看 Douglas Crockford 的 JavaScript 调查 了解更多详细信息。

Curly braces in javascript are used as shorthand to create objects. For example:

// Create an object with a key "name" initialized to the value "testing"
var test = { name : "testing" };
alert(test.name); // alerts "testing"

Check out Douglas Crockford's JavaScript Survey for more detail.

如若梦似彩虹 2024-10-09 06:31:26
var x = {title: 'the title'};

定义一个具有属性的对象文字。你可以这样做

x.title 

,它将评估为“标题;”

这是将配置传递给方法的常用技术,这就是这里发生的情况。

var x = {title: 'the title'};

defines an object literal that has properties on it. you can do

x.title 

which will evaluate to 'the title;

this is a common technique for passing configurations to methods, which is what is going on here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文