【发布时间】:2013-05-05 05:20:07
【问题描述】:
我在某处读到Object.__proto__ 和Object.prototype 指向同一个东西,Object.prototype 是标准方式,但昨晚我试图用Object.prototype 继承一个类,但它没有用然后我试图弄清楚Object.__proto__ 和Object.prototype 是否都指向同一个东西。令我惊讶的是
alert(Object.__proto__===Object.prototype);
在警告框中显示false
因此,为了弄清楚哪一个有效,我编写了以下代码
function Cx(){
this.objName="i m X";
this.prototype={ };
this.prototype.getMyName=function (){
alert(this.objName);
}
this.__proto__={ };
this.__proto__.hMyName=function(){
alert("I am hMyName");
}
}
function InheritCx(){
//var y=new Cx();
this.objName="I am InheritCx";
this.__proto__=new Cx();
}
y= new InheritCx();
y.hMyName();//displayes "I am hMyName" in alertbox when using chrome or mozilla
y.getMyName();//displays an error in chrome and mozilla
两者有什么区别,为什么标准代码不起作用? 此外,我有兴趣知道如何使原型继承在大多数浏览器(IE 6-8、9、10、chrome、firefox、mozilla、opera 和 webkit)中工作?
【问题讨论】:
-
Object.__proto__ === Function.prototype,至少在 Chrome 中(另见下面的评论) -
“我在某处读到
Object.__proto__和Object.prototype指向同一个东西……”。没有。Object是一个函数,所以Object.__proto__将是Function.prototype。 -
@FabrícioMatté 很酷。我们需要等待什么版本的 IE 死机才能真正使用它(严肃的问题)?
-
@JanDvorak 泄露的Windows Blue的IE(可能是IE11)支持
__proto__听说了。
标签: javascript prototype prototypal-inheritance