读书人

javascript构造函数的prototype默认是

发布时间: 2012-03-01 10:25:47 作者: rapoo

javascript构造函数的prototype默认是不是Object.prototype呢?若不是,则构造函数的prototype什么对象呢?
我是想通过下面的代码,找出javascript构造函数的prototype默认是哪个对象,可却发现不是不是Object,也不是Object.prototype,那是哪个对象呢?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN " "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=GB18030 ">
<title> Insert title here </title>
</head>
<body>
<script type= "text/javascript ">
function circle(x,y,r)
{
this.x = x;
this.y = y;
this.r = r;
}

if(circle.prototype == null){
alert( "NULL ");
}else{
//结果输出NOT NULL
alert( "NOT NULL ");
alert(circle.prototype);
alert(typeof circle.prototype);

if(circle.prototype == Object){
alert( "SAME TO OBJECT ");
}else{
//结果输出DIRRERENT
alert( "DIFFERENT TO OBJECT ");
}


if(circle.prototype == Object.prototype){
alert( "SAME TO OBJECT 'S PROTOTYPE ");
}else{
//结果输出DIRRERENT
alert( "DIFFERENT TO OBJECT 'S PROTOTYPE ");
}

}


</script>
</body>
</html>




[解决办法]

JScript code
<script   type= "text/javascript"> function circle(x, y, r) {     this.x = x;    this.y = y;    this.r = r;}if (circle.prototype == null){     alert("NULL"); } else {//结果输出NOT NULL    alert("NOT NULL"); //circle是Function的实例,Function的实例是带prototype的    if (circle.prototype == Object) {        alert("SAME TO OBJECT");    } else {    //结果输出DIRRERENT     //prototype貌似继承了Object.prototype,不过继承不代表===吧?    //证明继承的方法很简单delete circle.prototype.constructor;后在alert(circle.prototype.constructor)就会指向Object        alert("DIFFERENT TO OBJECT");    }             if(circle.prototype == Object.prototype){         alert( "SAME   TO   OBJECT 'S   PROTOTYPE ");     }else{      //结果输出DIRRERENT      //这里不解释了。。。看上面的解释就知道了。。。        alert( "DIFFERENT   TO   OBJECT 'S   PROTOTYPE");    }} </script> 

读书人网 >JavaScript

热点推荐