无法使用点表示法获得价值

Can't get value using dot notation

该代码与 WHITESPACE_ONLY 选项完美配合。但在高级模式下,点符号不起作用。但是括号符号仍然有效。

这是 JSON 对象:

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

代码如下:

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

问题出在哪里?

它为下一行返回未定义,因为您从未定义 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']); 

确保你 understand the differences between the compilation modes.

在ADVANCED_OPTIMIZATIONS中,闭包编译器重命名点分引用的属性,而不是使用引号引用的属性。示例:

原文:

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

已编译

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

由于 JSON 对象属性对编译器是隐藏的,您必须始终使用引号来访问它们。

// 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']);