gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:14:15 25 4
gpt4 key购买 nike

据此: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/

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