原创  JavaScirpt内置对象容易让人迷惑的地方 收藏

如果下面的一群alert对你来说很easy答对,那么,我这篇总结你可以不看了~

alert(typeof Function);        

alert(typeof (new Function));

alert(typeof Object);

alert(typeof Array);

alert(typeof (new Object));

alert(typeof (new Array));

alert(typeof Math);

alert(typeof (new Math));

JS是基于对象,预先内置了许多对象,有FunctionObjectArrayMathString等。其中Function比较特殊,是函数对象。函数对象的类型是function(注意:JS内置对象类型全部都为小写,如:functionobject,而函数对象实例的类型也是function,如下:

typeof Function==”function”

typeof (new Function)==”function”

而除函数对象之外的其他内置对象总体来说可以分为两类。一类是静态内置对象,一类是动态内置对象。静态内置对象是不通过new关键字直接就用的内置对象,如Math,我们每次用的时候都是直接用,如:Math.abs(-12);动态内置对象用之前必须先new出实例,如var date= new Date();

这两类对象是有区别的。

对于静态内置对象,对象类型为object,而实例对象是不允许的。如:

typeof Math==”Object”

typeof (new Math)           //对象不支持此操作

对于动态内置对象,对象类型为function,而实例对象是object。如:

typeof Object==”function”

typeof Array==”function”

typeof (new Object)==”object”

typeof (new Array)==”object”

那么开篇的答案应该明白了吧。如下:

alert(typeof Function);         //function

alert(typeof (new Function));   //function

alert(typeof Object);           //function

alert(typeof Array);            //function

alert(typeof (new Object));     //object

alert(typeof (new Array));      //object

alert(typeof Math);             //object

alert(typeof (new Math));       //对象不支持此操作

那么,看看下面的你知道答案不?

var s=new String();

var ss="haha";

alert(typeof String);

alert(typeof s); 

alert(typeof ss);

alert(typeof 1);

alert(typeof Number);

答案如下:

var s=new String();

var ss="haha";

alert(typeof String);    //function

alert(typeof s);         //object

alert(typeof ss);        //string

alert(typeof 1);         //number

alert(typeof Number);    //function

发表于 @ 2009年03月19日 15:12:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:如何算1-某一整数中所有1的个数

  • 发表评论
  • 评论内容:
  •  
Copyright © wenlz123
Powered by CSDN Blog