flash - 无类型变量相对于对象有什么好处? null 和 undefined 有什么区别?

标签 flash apache-flex actionscript-3 actionscript

据此:http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9f.html引用:

An untyped variable is not the same as a variable of type Object. The key difference is that untyped variables can hold the special value undefined , while a variable of type Object cannot hold that value.



但是,当我将其测试为:
var objTest:Object = 123; var untypedTest:* = 123; objTest = undefined; untypedTest = undefined; //This is understandable but why was the assignment even allowed? trace(objTest); // prints null trace(untypedTest); // prints undefined objTest=null; untypedTest = null; //This is also understandable ... both can store null trace(objTest); // prints null trace(untypedTest); // prints null //If they are null whey are they being equal to undefined? if(objTest==undefined) trace("obj is undefined"); if(untypedTest==undefined) trace("untyped is undefined"); //Because null is same as undefined! if(null==undefined) trace("null is same as undefined?");
两个问题:
  • 为什么 obj 允许赋值给 undefined? (不是大问题,因为它仍然打印为空)
  • 如果我们将 null 与 undefined 进行比较,结果为 true(即使 null 存储在对象中)。如果 null 和 undefined 相等,那么区分它们有什么意义?
  • 最佳答案

  • Flash 有类型转换来转换某些类型。

  • 一些示例:
    var i:int = NaN;
    trace (i); // 0
    

    或者:
    var b:Boolean = null;
    trace(b); // false
    

    所以当你分配 undefinedObject实例 Flash 将其转换为 null以同样的方式。
  • 您的比较在评估之前对不兼容的类型应用了类型转换 Boolean .

  • 您可以使用严格比较来获得 false :
    if(null === undefined)
        trace("Never traced: null is not the same as undefined!");
    

    关于flash - 无类型变量相对于对象有什么好处? null 和 undefined 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6980349/

    相关文章:

    linux - Google Chrome/Chromium 在 Ubuntu 12.10 上有错误

    c++ - 通过 UDP 从 Flex 客户端到 C++ 服务器的大文件传输

    actionscript-3 - 临时存储文件并在没有 Air 的 AS3 中再次加载它们

    flash - Flash Pixel Bender:音频处理的数字运算

    actionscript-3 - 在 AS3 中嵌入资源、位图缓存和位图转换

    flash - 为什么 Flash 对 CPU 的影响如此之大?

    actionscript-3 - 内存使用量较少的位图数据替代方案?

    javascript - 将第 3 方 Flash 组件集成到 Adob​​e Air 应用程序中?

    mysql - 从 adobe flex 应用程序更新 Mysql

    actionscript-3 - Flash/Flex Speex 音频解码,播放 speex 文件