函数的JavaScript参数中的花括号 [英] Curly braces inside JavaScript parameters for functions

查看:152
本文介绍了函数的JavaScript参数中的花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What do the curly braces surrounding JavaScript parameters 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",这是必需的.

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参数中的花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆