首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能用点表示法得到值

不能用点表示法得到值
EN

Stack Overflow用户
提问于 2016-04-02 22:16:30
回答 2查看 235关注 0票数 0

该代码非常适合WHITESPACE_ONLY选项。但是在高级模式下,点表示法不起作用。但括号符号仍然有效。

以下是JSON对象:

代码语言:javascript
复制
{
    'title' : 'The title',
    'type' : 'SIM',
    'description' : 'Build your own description.',
    'iconclass' : goog.getCssName('img-icons-bs')
}

以下是代码:

代码语言:javascript
复制
console.log('this.obj_ = ' + JSON.stringify(this.obj_));
console.log('this.obj_.iconclass = ' + this.obj_.iconclass);
console.log('this.obj_[iconclass] = ' + this.obj_['iconclass']);

输出:

代码语言:javascript
复制
> 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

问题出在哪里?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-04 01:06:16

确保你的理解编译模式之间的差异

在ADVANCED_OPTIMIZATIONS中,闭包编译器重命名由虚线符号引用的属性,而不使用引号重命名属性。示例:

原始

代码语言:javascript
复制
var foo = {};
foo.bar = foo['bar'] = 47;

编译

代码语言:javascript
复制
var a = {};
a.b = a.bar = 47;

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

代码语言:javascript
复制
// 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']);
票数 1
EN

Stack Overflow用户

发布于 2016-04-02 22:26:29

由于您从未定义过game,因此它将返回未定义的下一行。再检查一次输出。你读错了。

代码语言:javascript
复制
console.log('this.obj_[iconclass] = ' + this.game_['iconclass']); 

如果将行更改为以下内容,则对括号和点表示法都适用:

代码语言:javascript
复制
console.log('this.obj_.iconclass = ' + this.obj_.iconclass);
console.log('this.obj_[iconclass] = ' + this.obj_['iconclass']); 

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36379364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档