el@defiant ~ $ js
js> typeof boo
"undefined"
js> boo
typein:2: ReferenceError: boo is not defined
js> boo=5
5
js> typeof boo
"number"
js> delete(boo)
true
js> typeof boo
"undefined"
js> boo
typein:7: ReferenceError: boo is not defined
如果你在javascript中设置一个变量为undefined:
把这个放到myjs.html中:
<html>
<body>
<script type="text/JavaScript">
document.write("aliens: " + aliens);
document.write("typeof aliens: " + (typeof aliens));
var aliens = "scramble the nimitz";
document.write("found some aliens: " + (typeof aliens));
document.write("not sayings its aliens but... " + aliens);
aliens = undefined;
document.write("aliens deleted");
document.write("typeof aliens: " + (typeof aliens));
document.write("you sure they are gone? " + aliens);
</script>
</body>
</html>
输出如下:
aliens: undefined
typeof aliens: undefined
found some aliens: string
not sayings its aliens but... scramble the nimitz
aliens deleted
typeof aliens: undefined
you sure they are gone? undefined
function GenerateRandomString() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 50; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
var myVar = {}[GenerateRandomString()];