Você está na página 1de 2

In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such

as: var TestVar; alert(TestVar); //shows undefined alert(typeof TestVar); //shows undefined null is an assignment value. It can be assigned to a variable as a representatio n of no value: var TestVar = null; alert(TestVar); //shows null alert(typeof TestVar); //shows object From the preceding examples, it is clear that undefined and null are two distinc t types: undefined is a type itself (undefined) while null is an object. ================================================================================ ========== You can even compare a variable that is undefined to null or vice versa, and the condition will be true: undefined == null null == undefined You can explicitely set a variable to be undefined, but I highly advise against it. I recommend only setting variables to null and leave undefined the value for things you forgot to set. At the same time, I really encourage you to always se t every variable. JavaScript has a scope chain different than that of C-style la nguages, easily confusing even veteran programmers, and setting variables to nul l is the best way to prevent bugs based on it. Another instance where you will see undefined pop up is when using the delete op erator. Those of us from a C-world might incorrectly interpret this as destroyin g an object, but it is not so. What this operation does is remove a subscript fr om an Array or a member from an Object. For Arrays it does not effect the length , but rather that subscript is now considered undefined. var a = [ 'a', 'b', 'c' ]; delete a[1]; for (var i = 0; i < a.length; i++) WScript.Echo((i+".) "+a[i]); The result of the above script is: 0.) a 1.) undefined 2.) c You will also get undefined returned when reading a subscript or member that nev er existed. The difference between null and undefined is: JavaScript will never set anything to null, that's usually what we do. While we can set variables to undefined, we prefer null because it's not something that is ever done for us. When you're de bugging this means that anything set to null is of your own doing and not JavaSc ript. Beyond that, these two special values are nearly equivalent ================================================================================ =========== For a variable to receive a null value it must be assigned. null is used to indi cate an unknown or don't care value. undefined on the other hand is designed to indicate that the propery being accessed has never ben assigned a value. This di ffers from null. With null one is deliberately saying "I don't know what value this should have y

et" or "I don't care what value this is right now". OTH in undefined is really s aying "Are you sure you should be using this value it hasn't been assigned". ================================================================================ =========== The way I distinguish them is undefined being "I have not defined this value," a nd null being "I have defined this value, but I do not know or cannot figure out what the value should be." ================================================================================ ===========

Você também pode gostar