关于(javascript) "if"关键字的一个疑惑的地方
一直以来对if(obj)的用法有点疑惑,不知道如果obj是一个对象的时候(而不是简单的true/false),该怎么执行。在什么情况下该为true,什么情况下又该为false.
通过下面的小测试,了解到,当obj="",obj=undefined,obj=null,obj=NaN 的时候返回false,obj不为空的时候则返回true。
var obj ;//undefined
if(obj)alert('0:init');
obj = "";//\0
if(!obj)alert('1:\"\"');
obj = undefined;//undefined
if(!obj)alert('2:undefined');
obj = null;
if(!obj)alert("3:null");
obj = false ;
if(!obj)alert("4:false");
obj = true ;
if(!obj)alert("5:true");
obj = "string";
if(!obj)alert("6:string");
obj = NaN;
if(!obj)alert("7:NaN");
执行的结果:
1:""
2:undefined
3:null
4:fasel
7:NaN
当用var 定义一个变量的时候,如果不初始化,则默认值是undefined。
相关文档:
在prototype框架中的类继承实现机制
//为Object类添加静态方法:extend
Object.extend = function(destination, source) {
for(property in source) {
destination[property] = source[property];
}
return destination;
}
//通过Object类为每个对象添加方法 ......
<html>
<head>
<mce:script language="javascript"><!--
function old_page()
{
window.location = "http://www.jb51.net"
}
function replace()
{
window.location.replace("http://www.jb51.net")
}
function new_page()
{
window.op ......
第一步:在IE的“Internet设置”中选择“高级”——“安全”——“启用集成windows身份验证”。
第二步:在需要调试的页面的脚本中加上debugger; 然后运行这个页面,就会在这里停下来
例如:
function BtnSubmit_Click()
{
var aaa;
aaa='aaa';
debugg ......
此例子是javascript高级程序设计里的
放出来心备以后使用。上代码。
<html>
<head>
<title>Table Sort Example</title>
<mce:script type="text/javascript"><!--
function convert(sValue, sDataType) {
swi ......