関数の JavaScript 引数を囲む中括弧は何をしますか?
var port = chrome.extension.connect({name: "testing"});
port.postMessage({found: (count != undefined)});
関数の JavaScript 引数を囲む中括弧は何をしますか?
var port = chrome.extension.connect({name: "testing"});
port.postMessage({found: (count != undefined)});
中括弧はオブジェクト リテラルを表します。これは、データのキーと値のペアを送信する方法です。
したがって、この:
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"
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 調査をご覧ください。
var x = {title: 'the title'};
プロパティを持つオブジェクト リテラルを定義します。できるよ
x.title
これは「タイトル」に評価されます。
これは、構成をメソッドに渡すための一般的な手法であり、ここで行われていることです。