1.变量
命名规则:$ _或字母开头。
变量无类型,可以不初始化。不过是声明变量用var 来声明是个好习惯。
2.值的类型
原始值:存在栈中 包括Null, Undefined, Number, String, Boolean五种基本类型。
引用值:存在堆中, Object.
特别: typeof null返回object, null==undefined返回true;
常用Obejct对象属性: constructor构造函数。
prototype返回object的一个实例。
常用对象:
Number, String , Array, Date, Function, RegExp, HTML DOM.
3.运算符
在计算上与c语言家族很上
与java , c等编程语言有较大区别的例子:
var e=e||window.event; 是比较常见的声明event对象的方法。意思是如果e存在,e=e,否则e=window.event.
"Blue"<"alpha" 而"blue">"alpha"字符串的比较是比较字符码,B:66, b: 98, a: 97.
"25">3返回true "25"被转换成数字25. "a">3 返回false, 因为"a"转成数字会成为NaN,任何包含NaN的关系运算符将返回false;
4.类型检查
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>类型检查</title>
<style>
.code{font-family:Verdana, Geneva, sans-serif; min-width:500px;background:darkgrey;white-space:pre}
</style>
</head>
<body>
<div>类型检查的两种方法:
<p>1. typeof
返回值可能为number boolean string object function undefined.缺点是对object对象无法再细分。
</p>
例子:
<div class="exmple">
<div class="code">
function testTypeof(){
var str="bbb";
var str1=new String("bbb");
var num=10;
alert(0==false)
alert((str==str1)+","+(str===str1)); //true,false ==值相同 ===值和类型都要相同
alert(typeof str); //string
alert(typeof str1); //object
alert(typeof num); //number
}
</div>
<button οnclick="test(this)">运行</button>
</div>
<p>2. constructor属性, 为function
利用构造函数比较类型.用法 val.constructor == String, val是基本类型string,或者对象String 上面这句都返回true
</p>
例子:
<div class="exmple">
<div class="code">
function testConstructor(){
var str="bbb";
var str1=new String("bbb");
var num=1;;
var o=new Object();
alert(str.constructor== String); //true
alert(str1.constructor== String); //true
alert(num.constructor== Number); //true
alert(o.constructor== Object); //true
}
</div>
<button οnclick="test(this)">运行</button>
</div>
</div>
<script>
function test(oBtn)
{
var oDiv=oBtn.parentNode.childNodes[1];
var code=oDiv.innerHTML;
var fn=eval("("+code+")");
fn();
}
</script>
</body>
</html>
参考图书:<<javascript高级程序设计>><<精通javascript>>