1. 程式人生 > >javascript學習筆記—判斷值和函式的型別

javascript學習筆記—判斷值和函式的型別

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <script type="text/javascript">
	/*
	五種基本資料型別:
			number:數值
			string:字串
			boolean:布林
			null:空
			undefined:未定義

	引用資料型別:typeof
	
	資料型別轉換
		Number()、parseInt()和parseFloat()

	Boolean()轉換規則
	以下值為false:
			 null 
			 undefined 
			 空字串"" 
			 數值0 
			 為null的物件 
	*/

	<!--輸出值的型別-->
	var width;
	var arrlist=new Date();
	document.write(typeof("woanji")+"<br />"); //結果為string
	document.write(typeof(12)+"<br />"); //結果為number
	document.write(typeof(true)+"<br />"); //結果為boolean
	document.write(typeof(null)+"<br />"); //結果為object
	document.write(typeof(arrlist)+"<br />"); //結果為object
	document.write(typeof(width)+"<br />"); //結果為undefined
	
	function test(a,b){
		alert("sum:"+(a+b));
	}
	//判斷值或函式是否為某型別的物件,返回boolean型別
	document.write(test instanceof Object);//結果為true,說明函式本質是也是物件
	var sss = "123sdfg";
	document.write(sss instanceof String); //結果為false,說明sss不是string的物件
  </script>
 </head>

 <body>
  
 </body>
</html>