标题:javascript的prototype的问题
取消只看楼主
maximosi
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-12-31
 问题点数:0 回复次数:0 
javascript的prototype的问题
麻烦哪位师傅把输出都给解释一下 谢谢!包括出现undefine是因为没那个属性还是没赋值等原因也说一下

<!DOCTYPE HTML>
<html>
<head>
<title></title>
<script type="text/javascript">
function Pet()
{
    var owner="Mrs.Jones";
    var gender=undefined;
    this.setOwner=function(who){owner=who;}
    this.getOwner=function(){return owner;}
    this.setGender=function(sex){gender=sex;}
    this.getGender=function(){return gender;}
}
function Cat(){}
Cat.prototype=new Pet();
//Cat.prototype.constructor=Cat;
Cat.prototype.speak=function speak(){return("Meow");};

function Dog(){}
Dog.prototype=new Pet();
Dog.prototype.constructor=Dog;
Dog.prototype.speak=function speak(){return("Woof");};

</script>
</head>
<body>
<script type="text/javascript">
document.write("Object"+"<br/>");
var obj=new Object;
document.write(obj.prototype+"----1<br/>");
document.write(Object.prototype+"----2<br/>");
document.write(obj.constructor+"----3<br/>");
document.write(Object.constructor+"----4<br/>");
obj.prototype=Array;
document.write("obj.prototype:"+obj.prototype+"----5<br/>");
document.write(Object.prototype+"----6<br/>");
obj.constructor=String;
document.write("string  "+obj.constructor+"----7<br/>");
document.write(Object.constructor+"----8<br/>");
Object.prototype=Date;
document.write(obj.prototype+"----9<br/>");
document.write(Object.prototype+"----10<br/>");
Object.constructor=RegExp;
document.write(obj.constructor+"----11<br/>");
document.write(Object.constructor+"----12<br/>");
document.write("<br/>");

document.write("Pet"+"<br/>");
var pet=new Pet;
document.write(Pet.prototype+"----1<br/>");
document.write(Pet.constructor+"----2<br/>");
document.write(pet.prototype+"----3<br/>");
document.write(pet.constructor+"----4<br/>");
document.write("<br/>");

document.write("Cat"+"<br/>");
var cat=new Cat;
document.write(Cat.prototype+"----1<br/>");
document.write(Cat.constructor+"----2<br/>");
document.write(cat.prototype+"----3<br/>");
document.write(cat.constructor+"----4<br/>");
document.write("<br/>");

document.write("Dog"+"<br/>");
var dog=new Dog;
document.write(Dog.prototype+"----1<br/>");
document.write(Dog.constructor+"----2<br/>");
document.write(dog.prototype+"----3<br/>");
document.write(dog.constructor+"----4<br/>");
</script>
</body>
</html>



结果显示
Object
undefined----1
[object Object]----2
function Object() { [native code] }----3
function Function() { [native code] }----4
obj.prototype:function Array() { [native code] }----5
[object Object]----6
string function String() { [native code] }----7
function Function() { [native code] }----8
function Array() { [native code] }----9
[object Object]----10
function String() { [native code] }----11
function RegExp() { [native code] }----12

Pet
[object Object]----1
function Function() { [native code] }----2
undefined----3
function Pet() { var owner="Mrs.Jones"; var gender=undefined; this.setOwner=function(who){owner=who;} this.getOwner=function(){return owner;} this.setGender=function(sex){gender=sex;} this.getGender=function(){return gender;} }----4

Cat
[object Object]----1
function Function() { [native code] }----2
undefined----3
function Pet() { var owner="Mrs.Jones"; var gender=undefined; this.setOwner=function(who){owner=who;} this.getOwner=function(){return owner;} this.setGender=function(sex){gender=sex;} this.getGender=function(){return gender;} }----4

Dog
[object Object]----1
function Function() { [native code] }----2
undefined----3
function Dog(){}----4
搜索更多相关主题的帖子: undefined javascript function return title 
2013-12-31 14:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-425954-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.436630 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved