NND,好好的一段代码到我这里怎么就不灵光了呢?
<script language="javascript" type="text/javascript">
<!--
function class1(){
//构造函数
}
class1.prototype={
method:function(){
alert(1);
},
method2:function(){
alert("method2");
}
}
function class2(){
//构造函数
}
//让class2继承于class1
for(var p in class1.protytype){
class2.prototype[p]=class1.prototype[p];
}
//覆盖定义class1中的method方法
class2.prototype.method=function(){
alert(2);
}
//创建两个类的实例
var obj1=new class1();
var obj2=new class2();
//分别调用obj1和obj2的method方法
obj1.method();
obj2.method();
//分别调用obj1和obj2的method2的方法
obj1.method2();
obj2.method2(); //这里系统提示不支持此属性或方法
//-->
</script>
按道理来说运行后的结果应该是1,2,method2,method2 ,但是运行后前三个结果可以得到,第四个结果却得不到,说对象不支持此属性或方法,why?
问题点数:20、回复次数:4Top
1 楼zhaoxiaoyang(梅雪香@深圳)回复于 2006-12-01 09:13:32 得分 5
//看一看JavaScript权威指南5th中关于继承的实现
//Example 9-3. Subclassing a JavaScript class
// Here is a simple Rectangle class.
// It has a width and height and can compute its own area
function Rectangle(w, h) {
this.width = w;
this.height = h;
}
Rectangle.prototype.area = function( ) { return this.width * this.height; }
// Here is how we might subclass it
function PositionedRectangle(x, y, w, h) {
// First, invoke the superclass constructor on the new object
// so that it can initialize the width and height.
// We use the call method so that we invoke the constructor as a
// method of the object to be initialized.
// This is called constructor chaining.
Rectangle.call(this, w, h);
// Now store the position of the upper-left corner of the rectangle
this.x = x;
this.y = y;
}
// If we use the default prototype object that is created when we
// define the PositionedRectangle( ) constructor, we get a subclass of Object.
// To subclass Rectangle, we must explicitly create our prototype object.
PositionedRectangle.prototype = new Rectangle( );
// We create this prototype object for inheritance purposes, but we
// don't actually want to inherit the width and height properties that
// each Rectangle object has, so delete them from the prototype.
delete PositionedRectangle.prototype.width;
delete PositionedRectangle.prototype.height;
// Since the prototype object was created with the Rectangle( ) constructor,
// it has a constructor property that refers to that constructor. But
// we want PositionedRectangle objects to have a different constructor
// property, so we've got to reassign this default constructor property.
PositionedRectangle.prototype.constructor = PositionedRectangle;
// Now that we've configured the prototype object for our subclass,
// we can add instance methods to it.
PositionedRectangle.prototype.contains = function(x,y) {
return (x > this.x && x < this.x + this.width &&
y > this.y && y < this.y + this.height);
}Top
2 楼zhaoxiaoyang(梅雪香@深圳)回复于 2006-12-01 09:25:21 得分 5
<script language="javascript" type="text/javascript">
<!--
function class1(){
//构造函数
}
class1.prototype={
method:function(){
alert(1);
},
method2:function(){
alert("method2");
}
}
function class2(){
//构造函数
}
class2.prototype = new class1();
class2.prototype.constructor = class2;
//覆盖定义class1中的method方法
class2.prototype.method=function(){
alert(2);
}
//创建两个类的实例
var obj1=new class1();
var obj2=new class2();
//分别调用obj1和obj2的method方法
obj1.method();
obj2.method();
//分别调用obj1和obj2的method2的方法
obj1.method2();
obj2.method2(); //这里系统提示不支持此属性或方法
//-->
</script>Top
3 楼hansonboy(hansonboy)回复于 2006-12-01 10:10:10 得分 5
楼主的代码是可以,但真的有点奇怪,
for(var p in class1.prototype){
class2.prototype[p]=class1.prototype[p];
}
这几句代码我直拷下来不能, 重新自己打一次就可以,Top
4 楼hansonboy(hansonboy)回复于 2006-12-01 10:12:34 得分 5
终于看到原因了, 呵呵
for(var p in class1.protytype){
楼主你看看你打错什么了?
是prototype而不是protytypeTop




