2016-04-02 14 views
0

コードはWHITESPACE_ONLYオプションで完璧に機能します。しかし、ADVANCEDモードでは、ドット表記法は機能しません。しかし、ブラケット記法はまだ機能します。ここでドット表記を使用して値を取得できません。

{ 
    'title' : 'The title', 
    'type' : 'SIM', 
    'description' : 'Build your own description.', 
    'iconclass' : goog.getCssName('img-icons-bs') 
} 

コードです:ここで

はJSONオブジェクトである

console.log('this.obj_ = ' + JSON.stringify(this.obj_)); 
console.log('this.obj_.iconclass = ' + this.obj_.iconclass); 
console.log('this.obj_[iconclass] = ' + this.obj_['iconclass']); 

出力:

> this.obj_ = {"title":"The title","type":"SIM","description":"Build 
> your own description.","iconclass":"img-icons-r"} 
> this.obj_.iconclass = undefined 
> this.obj_[iconclass] = img-icons-r 

問題がありますか?

+0

もう一度お試しください。そして、あなたはほとんどのタイプミスをチェックしました。たとえ '.iconClass'のような大文字と小文字の違いがあったとしても? – SmokeyPHP

+0

@SmokeyPHP、はい、私はそれをチェックしました。 – Vadim

答えて

1

をあなたunderstand the differences between the compilation modesていることを確認します。

ADVANCED_OPTIMIZATIONSでは、closure-compilerはドット付き表記で参照されるプロパティの名前を変更し、引用符で囲んだ表記を使用してプロパティの名前を変更しません。例:

オリジナル

var foo = {}; 
foo.bar = foo['bar'] = 47; 

JSONオブジェクトのプロパティは、コンパイラから隠されているので、あなたは常に引用された表記法を使用してそれらにアクセスする必要があります

var a = {}; 
a.b = a.bar = 47; 

をコンパイル。

// This line is incorrect - the compiler can rename '.iconclass' 
console.log('this.obj_.iconclass = ' + this.obj_.iconclass); 

// This line is correct - ["iconclass"] is safe from renaming. 
console.log('this.obj_[iconclass] = ' + this.obj_['iconclass']); 
+0

チャドありがとうございます。私は何が間違っていたかを今理解する。 – Vadim

0

gameを一度も定義していないため、次の行には未定義が返されます。出力を再度確認してください。あなたは間違った方法でそれを読んだ。

console.log('this.obj_[iconclass] = ' + this.game_['iconclass']); 

あなたはそれがブラケットとドット表記の両方で動作する、以下の行を変更した場合:

console.log('this.obj_.iconclass = ' + this.obj_.iconclass); 
console.log('this.obj_[iconclass] = ' + this.obj_['iconclass']); 

enter image description here

+0

game_はタイプミスです。もともとobj_。問題は依然として存在する。 – Vadim

+0

は私のために働く。添付のスクリーンショットをご覧ください –

関連する問題