当前位置: 代码迷 >> Web前端 >> 懂得函数即对象
  详细解决方案

懂得函数即对象

热度:240   发布时间:2012-11-08 08:48:11.0
理解函数即对象
function T(name){
	this.name = name;
}
var a = new T('Jack');


//注意:__proto__属性在访问IE下无法访问。

alert(a.constructor)//function T(){...}
alert(T.prototype.constructor);//function T(){...}
alert(T.prototype.isPrototypeOf(a))//true.判定 a 的_proto_ 指向 T的prototype对象。
alert(a.__proto__ == T.prototype)//true

//进一步的,函数 T是 Function的一个实例
alert(T.constructor)//function Function() {[native code]}
alert(Function.prototype.isPrototypeOf(T))//true
alert(T.__proto__)//function (){}
alert(T.__proto__ == Function.prototype)//true

alert(T instanceof Function)//true
alert(T instanceof Object)//true

//同时,Function本身是 Object 的一个实例
alert(Function instanceof Object)//true
alert(Object.prototype.isPrototypeOf(Function))//true

//Object本身也是一个"函数",看起来它像是自己的一个实例.
alert(Object.prototype.isPrototypeOf(Object))//true

//但是 Object.__proto__ 并不等于 Object.prototype,这就不知道原因了。
alert(Object.__proto__ == Object.prototype)//false


//一般来说,函数prototype对象都是Object的实例,因此,__proto__指向Object的prototype对象。
alert(Object.prototype.isPrototypeOf(T.prototype))//true
alert(Object.prototype.isPrototypeOf(Function.prototype))//true

//而Obejct的prototype对象比较特别,它的__proto__值为null.
alert(Object.prototype.__proto__)//null
alert(Object.prototype.isPrototypeOf(Object.prototype))//false



图示:


  相关解决方案