javascript继承问题

来源:百度知道 编辑:UC知道 时间:2024/05/18 11:57:34
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type="text/javascript" >
function Person (name){
this.name = name;
if(typeof Person._initialized=="undefined"){
Person.prototype.say = function (){
alert(this.name);

};
Person._initialized = true;
}
}

</script>

<script type="text/javascript">
function WhitePerson (name){
Person.call(this,name);
this.name = name;
if(typeof WhitePerson._initialized=="undefined"){
WhitePerson.prototype.area = func

呵呵 你的继承还得学习下!! 给你最经典的一句:
代码如下:
function WhitePerson (name){
this.name = name;
Person.call(this,name);
if(typeof WhitePerson._initialized=="undefined"){
WhitePerson.prototype.area = function (){
alert("中国"+this.name);
};
WhitePerson._initialized =true;
}

}
WhitePerson.prototype = new Person ();
好好想想为什么吧!!

你用的Call,这个原理我不清楚。不过我想如果你使用复制的话,那是肯定可以的。
或者:你可以使用prototype赋值

你采用的是动态原型继承方式,可惜你没完全懂
动态原型继承方式采用的是对象冒充(this的特点)来继承构造函数的属性,而用原型链继承prototype对象的方法。
function WhitePerson (name){
Person.call(this,name); //对象冒充继承了person的属性
this.name = name;
if(typeof WhitePerson._initialized=="undefined"){
WhitePerson.prototype.area = function (){ //新的prototype属性
alert("中国"+this.name);
};
WhitePerson._initialized =true;
}
发现没?你一直没有继承基类person的prototype属性,也就是你并没有继承Person.prototype.say = function (){
alert(this.name);