<script>
function Retangle(w,h){
this.width=w;
this.height=h;
}Retangle.prototype.area=function(){return this.width*this.height}function PositionRetangle(x,y,w,h){
this.superclass(w,h);
this.x=x
this.y=y
}
PositionRetangle.prototype.superclass=Retangle
a=new PositionRetangle(4,9,5,7)
alert(a.area())</script>
怎么让a 弹出面积值35啊? 少写了什么吗
function Retangle(w,h){
this.width=w;
this.height=h;
}Retangle.prototype.area=function(){return this.width*this.height}function PositionRetangle(x,y,w,h){
this.superclass(w,h);
this.x=x
this.y=y
}
PositionRetangle.prototype.superclass=Retangle
a=new PositionRetangle(4,9,5,7)
alert(a.area())</script>
怎么让a 弹出面积值35啊? 少写了什么吗
function Retangle(w,h){
this.width=w;
this.height=h;
Retangle.prototype.area=function(){return this.width*this.height};
}function PositionRetangle(x,y,w,h){
this.superclass=new Retangle(w,h);
this.x=x
this.y=y
}
var a=new PositionRetangle(4,9,5,7);
alert(a.superclass.area())
</script>
不知道行不行
你的代码真混乱。而且很不规范。。
function Retangle(w,h){
this.width=w;
this.height=h;
Retangle.prototype.area=function(){return this.width*this.height};
}function PositionRetangle(x,y){
this.x=x;
this.y=y;
}
PositionRetangle.prototype=new Retangle(5,7);
var a=new PositionRetangle(4,9);
alert(a.area());
</script>
function Retangle(w,h){
this.width=w;
this.height=h;
Retangle.prototype.area=function(){return this.width*this.height};
}function PositionRetangle(x,y,w,h){
Retangle.call(this,w,h);
this.x=x;
this.y=y;
}
PositionRetangle.prototype=new Retangle();
var a=new PositionRetangle(4,9,5,7);
alert(a.area());
</script>
一起传参的话 就这样~
可试着写代码是不行啊?
this.width=w;
this.height=h;
}Retangle.prototype.area=function(){return this.width*this.height}
//因为area方法需要的是width、height这两个属性,所以赋上值再调用area方法就直接可以了。
function PositionRetangle(x,y,w,h){
this.width=w;
this.height=h;
this.x=x
this.y=y
}
//js因为是原型继承,所以想要让PositionRetangle具有area方法,就必须要把
//prototype定义为一个具体的Retangle对象
PositionRetangle.prototype=new Retangle();
a=new PositionRetangle(4,9,5,7)
alert(a.area());
</script>
js没有提供直接的继承方式,要实现继承需要自己写继承方法
一般可以用prototype属性达到继承的目的,在网上搜一下js的继承,代码大把,还有一些框架也都提供继承的函数
1. area() 是从父类继承的
2. 继承的同时又想访问到父类的方法,类似java中的super.方法名
function Retangle(w,h){
this.width=w;
this.height=h;
this.area = function() {
return this.width * this.height;
}
} function PositionRetangle(x,y,w,h) {
this.x = x;
this.y = y;
this.width=w;
this.height=h;
this.superclass = new Retangle(this.width, this.height);
} PositionRetangle.prototype = new Retangle() ; var a = new PositionRetangle(1,1,5,7);
alert(a.area());
alert(a.superclass.area());JS有自己的继承机制,推荐你看 《JavaScript高级程序设计》第六章
function Retangle(w,h){
this.width=w;
this.height=h;
}Retangle.prototype.area=function(){return this.width*this.height};function PositionRetangle(x,y,w,h){
this.superclass(w,h);
this.x=x
this.y=y
}
PositionRetangle.prototype=new Retangle()
PositionRetangle.prototype.superclass=Retanglevar a=new PositionRetangle(4,9,5,7);
alert(a.area())
</script>
alert(a.height) alert(a.x) alert(a.y)都可以取值