关于(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类为每个对象添加方法 ......
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head ......
Event属性和方法:
1. type:事件的类型,如onlick中的click;
2. srcElement/target:事件源,就是发生事件的元素;
3. button:声明被按下的鼠标键,整数,1代表左键,2代表右键,4代表中键,如果按下多个键,酒把这些值加起来,所以3就代表左右键同时按下;(firefox中 0代表左键,1代表中间键,2代表右键) ......
JS层
// 定义一个全局
var xmlHttp;
// 返回一个xmlHttpRequest对象
function createXMLHttpRequest() {
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
......