辞書に toString を追加する
重要性: 5
任意の key/value
ペアを格納するために Object.create(null)
として生成されたオブジェクト dictonary
があります。
その中にメソッド dictionary.toString()
を追加してください。それはカンマ区切りのキーのリストを返します。あなたの toString
はオブジェクト上の for..in
で現れるべきではありません。
次のように動作します:
let
dictionary =
Object.
create
(
null
)
;
// dictionary.toString メソッドを追加するあなたのコード
// データの追加
dictionary.
apple =
"Apple"
;
dictionary.
__proto__ =
"test"
;
// __proto__ はここでは通常のプロパティキー
// ループでは apple と __proto__ だけです
for
(
let
key in
dictionary)
{
alert
(
key)
;
// "apple", then "__proto__"
}
// 実行時のあなたの toString です
alert
(
dictionary)
;
// "apple,__proto__"
このメソッドは Object.keys
を使ってすべての列挙可能なキーを取り、そのリストを出力します。
toString
を非列挙型にするために、プロパティディスクリプタを使って定義しましょう。Object.create
の構文は、2番目の引数としてプロパティディスクリプタのオブジェクトを指定することができます。
let
dictionary =
Object.
create
(
null
,
{
toString
:
{
// toString プロパティの定義
value
(
)
{
// 値は関数です
return
Object.
keys
(
this
)
.
join
(
)
;
}
}
}
)
;
dictionary.
apple =
"Apple"
;
dictionary.
__proto__ =
"test"
;
// ループでは apple と __proto__ だけです
for
(
let
key in
dictionary)
{
alert
(
key)
;
// "apple", then "__proto__"
}
// toString によるカンマ区切りのプロパティのリスト
alert
(
dictionary)
;
// "apple,__proto__"
ディスクリプタを使ってプロパティを作成するとき、そのフラグはデフォルトでは false
です。なので、上のコードで。dictionary.toString
は非列挙型です。