Discuss / JavaScript / arr ----> Array.prototype ----> Object.prototype ----> null 这个原型链,我没看明白!

arr ----> Array.prototype ----> Object.prototype ----> null 这个原型链,我没看明白!

Topic source

王奋浩2015

#1 Created at ... [Delete] [Delete and Lock User]

arr是数组变量的引用,而Array.prototype是Array函数的原型吧?arr这个引用是指向Array函数的原型吗?就是说 arr === Array.prototype吗?如果不是,那--->箭头是什么意思呢? Array.prototype也是指向 Object.prototype吗?

colorsumer

#2 Created at ... [Delete] [Delete and Lock User]

不知道你搞明白了没有,但是你的问题对我启发很大,这是实验的结果以及不知道对不对的解读:

1.以Array为构造器创建的对象,构造器是Array,原型引用指向Array的原型。

var arr=[1,2,3]
alert(arr.__proto__===Array.prototype);//true
alert(arr.constructor===Array);//true

var arr2=new Array();
alert(arr2.__proto__===Array.prototype);//true
alert(arr2.constructor===Array);//true

2.以Array为原型创建的对象,原型引用指向Array

var a=Object.create(Array);
alert(a.__proto__===Array);//true

3.Array是function Array(),Object是function Object() 。它们有共同的构造器function Function()。function Function()是创建函数的函数,是所有函数的构造器。

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

4.Obejct函数的原型,就是以Object函数为构造器创建的抽象对象: object: Object

alert(Object.prototype);//object:Object
alert(typeof Object.prototype);//object
alert(Object.prototype.constructor);//undefined

5.同样的,Array函数的原型,就是以Array函数为构造器创建的抽象对象: object: Array。但是这个对象的toString()方法的返回值是空字符串‘’。同时,这个抽象的Array对象不等于[],也不等于null。

alert(Array.prototype);//
var s=Array.prototype.toString();
alert(s==='');//true
alert(typeof Array.prototype);//object
alert(Array.prototype.constructor); //function Array (){[native code]}
alert(Array.prototype===null);//false
alert(Array.prototype===[]);//false

6.最后,Array函数的原型的原型引用指向Object函数的原型。也就是说抽象对象Array的proto属性指向抽象对象Object。同时,包括函数(的原型对象)在内的其他对象与Array情况相同。

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

7.还有,一切函数的原型都是对象,一切对象都以函数为构造器。普通对象或者普通函数的原型,其原型引用同样指向Object函数的原型(抽象对象Object)。普通对象或者普通函数的原型,其toString()方法的返回值与抽象对象Object相同,同是‘object Object’,但并非相同的对象。

function b(){return 1;};
alert(b.constructor);//function Function(){[native code]}
alert(b.prototype);//object Object
alert(Object.prototype);//object Object
alert(b.prototype.toString()===Object.prototype.toString());//true
alert(b.prototype===Object.prototype);//false
alert(b.prototype.__proto__===Object.prototype);//true

var c=new Function();
alert(c);//function anonymous(){}
alert(c.constructor);//function Function(){[native code]}
alert(c.prototype.__proto__===Object.prototype);//true

var d={};
alert(d.constructor);//function Object(){[native code]}
alert(d);//object Object
alert(d.__proto__===Object.prototype);

  • 1

Reply