Curly braces inside JavaScript arguments for functions(函数的 JavaScript 参数中的花括号)
问题描述
围绕 JavaScript 函数参数的花括号有什么作用?
What do the curly braces surrounding JavaScript arguments for functions do?
var port = chrome.extension.connect({name: "testing"});
port.postMessage({found: (count != undefined)});
推荐答案
花括号表示一个对象字面量.这是一种发送数据键/值对的方式.
The curly braces denote an object literal. It is a way of sending key/value pairs of data.
所以这个:
var obj = {name: "testing"};
这样用来访问数据.
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.
这在 "a-key"
的情况下是必需的.
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"
这篇关于函数的 JavaScript 参数中的花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:函数的 JavaScript 参数中的花括号


- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01