2011-08-08 2 views
7

: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9f.html 인용구 :개체에 대한 유형이 지정되지 않은 변수의 이점은 무엇입니까? null과 undefined의 차이점은 무엇입니까? 이에 따라

유형이 지정되지 않은 입력 변수가 객체의 변수와 동일하지 않다. 주요 차이점은 형식이 지정되지 않은 변수는 특수 값을 정의되지 않은 상태로 유지할 수 있지만 Object 유형의 변수는 해당 값을 보유 할 수 없다는 것입니다.

그러나 내가로 테스트 할 때 :

 

      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가 반환됩니다 (Object에 null이 저장된 경우에도 마찬가지 임). 동등한 경우 null과 undefined의 차이점은 무엇입니까?

답변

10
  • 플래시 형식 변환이 있습니다. 그

일부 샘플 :

var i:int = NaN; 
trace (i); // 0 

또는 :

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

플래시 null 같은 방식으로 변환 undefined 예를 Object에 당신이 할당하고 그래서.

  • Boolean을 평가하기 전에 호환되지 않는 유형의 비교 유형 변환을 사용하십시오.

당신은 false을 가지고 엄격한 비교를 사용할 수 있습니다

if(null === undefined) 
    trace("Never traced: null is not the same as undefined!"); 
+1

굉장한 ... 완벽한 설명 – basarat

+1

또한 키가 객체에 있는지 확인하기 위해 undefined를 사용합니다. 즉, if (myObj [ "someKey"] == undefined) // 추가하십시오. 체크에 null을 사용할 수도 있지만 누군가 null을 값으로 저장하려고 할 때 중단됩니다. 'myObj [ "someKey"] = null; // 유효 ' – divillysausages

+0

@divillysausages,'undefined'를 사용하는 것이 더 이상 사용되지 않습니다. 혼동을 막기 위해서 (때문에), null가 아니면 안됩니다. '값 없음'으로 키를 저장해야하는 경우 '0'또는 '거짓'만 사용하십시오 (값이없는 키의 위치는 무엇이겠습니까?). –

0

는 일부 값은 자동으로 비교 또는 지정을 위해 변환됩니다.

undefinedObject으로 승격 될 때 으로 변환된다. 따라서 기본적으로 수행 된 작업은 실제로 Object(null) == Object(undefined)이고 그 작업은 null == null이기 때문에 null == undefined입니다.

그러나 엄격한 비교를 수행하면 변환되지 않으므로 동일하지 않습니다. 즉 null === undefined은 false를 반환합니다.

관련 문제