typeof and instanceof

typeof:返回一个变量的类型,一般返回 number,boolean,string,function,object,undefined。

instanceof : 判断某个变量是否是某个对象的实例。

function test(a,b){
return a+b;
}

var a = new test();

console.log( a instanceof test);
console.log(typeof test=="function");
console.log(typeof a=="object");
console.log(typeof a=="function");

结果是:true,true,true,false

 

typeof

typeof 運算子可按下列兩種方式來使用︰

1. typeof operand
2. typeof (operand)

typeof 運算子可返回字串,這些字串指出未求值的運算元的類型。operand 是指字串、變數、關鍵字、物件,其類型可被 typeof 返回。括弧並非必要的。

假設你定義了以下變數︰

var myFun = new Function("5+2")
var shape="round"
var size=1
var today=new Date()

typeof 運算子對以下變數返回以下結果︰

typeof myFun 返回的是 function
typeof shape 返回的是 string
typeof size 返回的是 number
typeof today 返回的是 object
typeof dontExist 返回的是 undefined

對於 truenull 關鍵字而言,typeof 運算子返回以下結果︰

typeof true 返回的是 boolean
typeof null 返回的是 object

對於數字或字串而言,typeof 運算子返回以下結果︰

typeof 62 返回的是 number
typeof 'Hello world' 返回的是 string

對於屬性值而言,typeof 運算子返回屬性裡的值的類型︰

typeof document.lastModified 返回的是 string
typeof window.length 返回的是 number
typeof Math.LN2 返回的是 number

對於方法和函數而言,typeof 運算子返回以下結果︰

typeof blur 返回的是 function
typeof eval 返回的是 function
typeof parseInt 返回的是 function
typeof shape.split 返回的是 function

對於預先定義的物件而言,typeof 運算子返回以下結果︰

typeof Date 返回的是 function
typeof Function 返回的是 function
typeof Math 返回的是 function
typeof Option 返回的是 function
typeof String 返回的是 function
posted @ 2011-04-23 09:07  e.e.p  阅读(288)  评论(0编辑  收藏  举报