2014-06-19 35 views
0

在extjs4.2中;Extend的构造函数找不到配置参数?

在下面的代码中,它在chrome的控制台上显示“Uncaught ReferenceError:config is not defined”。

这可能是 中的问题Vehicle.prototype.constructor.call(this,config); 但是,为什么它不能通过配置。

为什么? 有人可以帮助我吗?

谢谢。

function Vehicle(config){ 
    this.x=config.x; 
    this.y=config.y; 
} 

Vehicle.prototype.move=function(dx,dy){ 
    this.x +=dx; 
    this.y +=dy; 
}; 

Vehicle.prototype.toString=function(){ 
    return "point:"+x+","+y; 
}; 

var Car=Ext.extend(Vehicle,{ 
    constructor:function(){ 
     Vehicle.prototype.constructor.call(this,config); 
     this.color=config.color; 
    },move:function(dx){ 
     this.x=dx; 
    },toString:function(){ 
     var str="Car is "+ this.x + " miles away from the origial position."; 
     str +=" this car is :"+this.colr; 
     return str; 
    } 

}); 

var carConfig={ 
    x:10, 
    y:0, 
    color:"white" 
}; 

var car= new Car(carConfig); 
car.move(150); 
console.info(car.toString()); 
+0

你可以做一个小提琴? – edhedges

+0

你为什么要写这样的课程? Ext提供了一个完整的类系统来抽象出很多这样的东西。 –

回答

0

所有你缺少的是你的构造函数的参数。

我做了一个小提琴展现雅:http://jsfiddle.net/edhedges/JkPLL/

function Vehicle(config){ 
    this.x=config.x; 
    this.y=config.y; 
} 

Vehicle.prototype.move=function(dx,dy){ 
    this.x +=dx; 
    this.y +=dy; 
}; 

Vehicle.prototype.toString=function(){ 
    return "point:"+x+","+y; 
}; 

var Car=Ext.extend(Vehicle,{ 
    constructor:function(config){ // missing config here 
     Vehicle.prototype.constructor.call(this,config); 
     this.color=config.color; 
    },move:function(dx){ 
     this.x=dx; 
    },toString:function(){ 
     var str="Car is "+ this.x + " miles away from the origial position."; 
     str +=" this car is :"+this.colr; 
     return str; 
    } 

}); 

var carConfig={ 
    x:10, 
    y:0, 
    color:"white" 
}; 

var car= new Car(carConfig); 
car.move(150); 
console.info(car.toString()); 
+0

亲爱的主持人:Thx为您提供帮助!我得到它 – wayout

+0

@wayout没问题。你可能要考虑埃文Tromboli在他对你的问题的评论中所说的话。我从来没有使用Ext JS,因此我不知道最佳实践,但他似乎认为有更好的方法来构建代码。 – edhedges