动作脚本:未定义的公共变量?
我有一个这样的类..
public class Doc {
public function Doc():void {}
public var myVar:Boolean;
}
我如何知道 myVar 持有的值是否默认为 false,或者有人为其分配了 false ?!?不是有一个未定义的状态吗?我怎样才能实现这样的目标?
I have a class like this..
public class Doc {
public function Doc():void {}
public var myVar:Boolean;
}
How can I know if the value held by myVar is default false, or someone has assigned false to it ?!? Isn't there an undefined state? How can I achieve such a thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 myVar 设为属性并使用另一个变量来检查它是否已显式设置。
Make myVar a property and use another variable to check if it's been set explicitly.
你不能使用布尔值,它默认为 false 且 false === false。
您不能严格键入变量,然后使用 getter 和 setter 来保护类型。
现在,当未设置 myVar 时,它应该 === null,之后您应该只能将其设置为布尔值。
但这感觉有点老套,我想知道为什么你需要区分它们。
You can't with a boolean, it defaults to false and false === false.
You'd could not strictly type the variable and then use a getter and setter to protect the type
Now, when its not set myVar should === null, and you should only be able to set it to a boolean after that.
But it feels a bit hacky, and I wonder why you need to tell the difference.