function func(){}
alert(func.prototype);//[object Object]
alert(func.__proto__);//function (){[native code]}
b=new func;
alert(b.__proto__);//[object Object]
func.prototype=Array;//继承后
alert(b.__proto__);//[object Object]
alert(func.prototype);//function Array(){[native code]}
alert(func.__proto__);//function (){[native code]}
a=new Array;
alert(a.__proto__);//空,为什么不是[object Object]???
alert(Array.prototype);//空,为什么不是function Object(){[native code]}???
Array.prototype=new Object;//继承后
alert(Array.prototype);//空??为什么???
Array或其他引用类型怎么继承Object的所有属性和方法???
先回答简单的问题,
alert
显示的是一个对象toString()之后的返回值。你看,下面,只要调用的是同一个toString
他们的输出是相同的。另外,想看对象结构的话不要用alert,可以console中的函数。
Array.prototype
应该是叫做“数组的原型”。在 JS 中所有使用new
创建的对象,都会生成一个以这个函数为原型的对象,比如arr = new Array
。表现就是arr.__proto__ === Array.prototype
。只是new Object,new Array,new Function, new RegExp
等都有其他的写法。当访问一个对象的属性,比如
arr.toString
,其实访问的是arr.toString? arr._proto_.toString? arr._proto_._proto_.toString?....
(按照这个顺序查找到的第一个)。又因为arr.__proto__ === Array.prototype
,而Array.prototype
具有toString
,所以arr.toString
访问的就是这里定义的那个函数,这样就实现了继承。在浏览器里最初的这种关系的设定是(黄色的箭头),Array的地位其实和下图中的Fish是一样的,只是Array,Array.prototype内置了一些属性和方法:

P.S.:
_proto_
这个属性在标准和书里面叫做[[prototype]]