ITEEDU

javascript深入Function和类型

类型的类型

类型是Function的实例。

alert( String instanceof Function);//true
alert( Number instanceof Function);//true
alert( Boolean instanceof Function);//true
alert( Object instanceof Function);//true
alert( Function instanceof Function);//true

alert(typeof String);//function
alert(typeof  Number);//function
alert(typeof  Boolean);//function
alert(typeof  Object);//function
alert(typeof  Function);//function

类型的内容

alert( String);//function String() {[native code]}
alert( Number);//function Number() {[native code]}
alert( Boolean);//function Boolean() {[native code]}
alert( Object);//function Object() {[native code]}
alert( Function);//function Function() {[native code]}

类型的__proto__属性

alert( String.__proto__);//function(){}
alert( Number.__proto__);//function(){}
alert( Boolean.__proto__);//function(){}
alert( Object.__proto__);//function(){}
alert( Function.__proto__);//function(){}
alert( String.__proto__===Function.prototype);//true
alert( Number.__proto__===Function.prototype);//true
alert( Boolean.__proto__===Function.prototype);//true
alert( Object.__proto__===Function.prototype);//true
alert( Function.__proto__===Function.prototype);//true

类型都是由一个构造函数创建的,这个构造函数的prototype为function(){}。

alert( Function.prototype);//function(){}

再次说明类型是Function的实例。

类型的prototype

alert( String.prototype);//''
alert( Number.prototype);//0
alert( Boolean.prototype);//false
alert( Object.prototype);//[object Object]
alert( Function.prototype);//function(){}

类型的prototype决定了其创建对象的__proto__属性,可以看new关键字的作用。 类型的区别从这里开始。

alert(typeof  String.prototype);//object
alert(typeof  Number.prototype);//object
alert(typeof  Boolean.prototype);//object
alert(typeof  Object.prototype);//object
alert(typeof  Function.prototype);//function

类型的constructor

alert(String.hasOwnProperty('constructor'));//false
alert(String.__proto__.hasOwnProperty('constructor'));//true

alert(String.constructor);//function Function() {[native code]}
alert(Number.constructor);//function Function() {[native code]}
alert(Boolean.constructor);//function Function() {[native code]}
alert(Object.constructor);//function Function() {[native code]}
alert(Function.constructor);//function Function() {[native code]}

类型都是用function Function() {[native code]}创建的。function Function() {[native code]}是什么呢?

alert(Function); //function Function() {[native code]}
alert(String.constructor===Function);//true
alert(Number.constructor===Function);//true
alert(Boolean.constructor===Function);//true
alert(Object.constructor===Function);//true
alert(Function.constructor===Function);//true

再次说明了类型是Function的实例。

Function

//一个环结构
alert(Function.prototype);//function(){}
alert(Function.__proto__===Function.prototype)//true
alert(Function.prototype.constructor===Function)//true
//一个奇怪的对象,是函数类型,但不是由Function创建。
alert(Function.prototype instanceof Object);//true
alert(Function.prototype instanceof Function);//false
alert(typeof Function.prototype);//function
// Function也是对象
alert(Function.prototype.__proto__)//[object Object]
alert(Function.prototype.__proto__===Object.prototype)//true
alert(Function.prototype.__proto__===Number.prototype)//false
alert(Function.prototype.__proto__.__proto__);//null

总结

有一个神秘对象function(){},它没有prototype属性,所以不能创建对象;它是function类型但不是Function实例,它的__proto__为object对象和Object.prototype是一个对象。

Function的__proto__为function(){},function(){}的constructor为Function,这是函数对象的顶层结构,因为函数对象也是由函数对象创建的,上层一定是一个环。

所有类型的__proto__为function(){},而function(){}的constructor为Function,说明类型是由Function创建的。

alert(String.prototype);//空
alert(String.prototype===Object.prototype);//false