typeof 运算符

描述
返回一个用来表示表达式的数据类型的字符串。
语法
typeof [ ( ] expression [ ) ] ;

expression 参数是需要查找类型信息的任意表达式 。

说明
typeof 运算符把类型信息当作字符串返回。typeof 返回值有六种可能: "number," "string," "boolean," "object," "function," 和 "undefined."

typeof 语法中的圆括号是可选项。

Js代码
  1. alert(typeof (5));  //<SPAN>number</SPAN>   
  2.   
  3. alert(typeof (true));  //<SPAN>boolean</SPAN>   
  4.   
  5. alert(typeof ("abc"));  //<SPAN><SPAN>string</SPAN>   
  6. /SPAN>  

---------------------------------------------------------------------------------------------------

 

instanceof 运算符

描述
返回一个 Boolean 值,指出对象是否是特定类 的一个实例。
语法
result = object instanceof class

instanceof 运算符的语法组成部分如下:

 

部分 描述
result 任何变量 。
object 任何对象表达式 。
class 任何已定义的对象类。

 

说明
如果 objectclass 的一个实例,则 instanceof 运算符返回 true 。如果 object 不是指定类的一个实例,或者 objectnull ,则返回 false

下面的例子举例说明了 instanceof 运算符的用法:

 

function objTest(obj)
{
  var i, t, s = "";            // 创建变量。
  t = new Array();             // 创建一个数组。
  t["Date"] = Date;            // 充填数组。
  t["Object"] = Object;
  t["Array"] = Array;
    for (i in t)
    {
      if (obj instanceof t[i]) // 检查 obj 的类。
      {
        s += "obj is an instance of " + i + "\n";
      }
      else 
      {
        s += "obj is not an instance of " + i + "\n";
      }
  }
  return(s);                   // 返回字符串。
}  

var obj = new Date();
response.write(objTest(obj));

 

 

 

 

 

 

javascript中instanceof和typeof解释

 

typeof用以获取一个变量的类型,typeof一般只能返回如下几个结果:number,boolean,string,function,object,undefined。我们可以使用typeof来获取一个变量是否存在,如if(typeof a!="undefined"){},而不要去使用if(a)因为如果a不存在(未声明)则会出错,对于Array,Null等特殊对象使用typeof一律返回object,这正是typeof的局限性。

 如果我们希望获取一个对象是否是数组,或判断某个变量是否是某个对象的实例则要选择使用instanceof。instanceof用于判断一个变量是否某个对象的实例,如var a=new Array();alert(a instanceof Array);会返回true,同时alert(a instanceof Object)也会返回true;这是因为Array是object的子类。再如:function test(){};var a=new test();alert(a instanceof test)会返回true。

 谈到instanceof我们要多插入一个问题,就是function的arguments,我们大家也许都认为arguments是一个Array,但如果使用instaceof去测试会发现arguments不是一个Array对象,尽管看起来很像。

另外:

测试 var a=new Array();if (a instanceof Object) alert('Y');else alert('N');
得'Y’

但 if (window instanceof Object) alert('Y');else alert('N');

得'N'

所以,这里的instanceof测试的object是指js语法中的object,不是指dom模型对象。

使用typeof会有些区别
alert(typeof(window) 会得 object

摘自:http://www.scriptlover.com/post/332

 

 

 

 

typeof return a data type .
instanceof return a boolen for object is instanced .

 

==================================================

 

typeof 返回值有六种可能: "number," "string," "boolean," "object," "function," 和 "undefined."

instanceof返回一个 Boolean 值,指出对象是否是特定类的一个实例

JScript code
function Student(name,age){ this.name=name; this.age=age; } function Man(name,age){ this.name=name; this.age=age; } var stu=new Student("dc",24); var man1=new Man("mm",34); alert (stu instanceof Student); //返回true alert(man1 instanceof Student); //返回false alert(man1 instanceof Man); //返回true alert (typeof(stu)); //返回object alert(typeof(man1)); //返回 object


不知道你是否能看懂

 

====================================================

 

typeof效率比较高 
instanceof 适用的范围很广
这2者 看情况使用

 

 

摘自:http://topic.csdn.net/u/20080515/13/b23b3e0b-c247-41f6-816d-2df5d2719d93.html

 

 

 

 

 

 

 

 

 

posted on 2009-07-19 17:26  钱途无梁  阅读(648)  评论(0编辑  收藏  举报