gpt4 book ai didi

javascript - 无法使用点表示法获取值

转载 作者:行者123 更新时间:2023-12-03 07:17:04 24 4
gpt4 key购买 nike

该代码与 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

问题出在哪里?

最佳答案

确保您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']);

关于javascript - 无法使用点表示法获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36379364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com