结构和字典有什么区别?
What is the difference between struct and dict?
这两个术语在documentation中用得很多。 struct
的概念很容易理解,即属性固定的对象,但是我们有 dict
可以有任意数量的属性,所以它与普通的 Object
有何不同?为什么此功能需要单独的类型?
我认为关键区别在于 documentation page
By using @struct, you know that the compiler will rename all properties safely, because you can't use bracket access. By using @dict, you know that the properties will have the same name after compilation.
(了解闭包编译器的关键是properties accessed with bracket notation are not renamed)。
使用闭包编译器以您指定的方式限制您对 JavaScript 的使用。您是在告诉编译器在您编写的代码违反您使用 @struct
和 @dict
.
等注释设置的限制时警告您
@dict
确实如你所说"like a normal Object
"。通过使用 @dict
,您告诉编译器您将向对象添加属性,并且您不希望将这些属性重命名为最小名称。
相比之下,@struct
通常用于 class,您不关心编译器将属性重命名为什么。您也不希望向这样的对象添加 属性,因此如果您这样做应该是一个错误。
顺便说一句,page about struct and dict 上的示例在我看来有点难以理解,所以如果您仍然对该页面上的任何内容感到困惑,请随时询问更多内容。
这两个术语在documentation中用得很多。 struct
的概念很容易理解,即属性固定的对象,但是我们有 dict
可以有任意数量的属性,所以它与普通的 Object
有何不同?为什么此功能需要单独的类型?
我认为关键区别在于 documentation page
By using @struct, you know that the compiler will rename all properties safely, because you can't use bracket access. By using @dict, you know that the properties will have the same name after compilation.
(了解闭包编译器的关键是properties accessed with bracket notation are not renamed)。
使用闭包编译器以您指定的方式限制您对 JavaScript 的使用。您是在告诉编译器在您编写的代码违反您使用 @struct
和 @dict
.
@dict
确实如你所说"like a normal Object
"。通过使用 @dict
,您告诉编译器您将向对象添加属性,并且您不希望将这些属性重命名为最小名称。
相比之下,@struct
通常用于 class,您不关心编译器将属性重命名为什么。您也不希望向这样的对象添加 属性,因此如果您这样做应该是一个错误。
顺便说一句,page about struct and dict 上的示例在我看来有点难以理解,所以如果您仍然对该页面上的任何内容感到困惑,请随时询问更多内容。