[英]Accessing properties in parent class
我正在嘗試制作一個由多個類繼承的父數據訪問層類。
家長班:
var DataAccess = function() {
this.Save = function(){
alert(this.ListName); //works
SaveLogic(this.Id); //doesnt work
}
}
兒童班:
var Job = function(){
Job.prototype.ListName = 'MyList'; //works
this.Save = function(){
Job.prototype.Save().call(this);
//specific Job Save logic
}
}
Job.prototype = new DataAccess();
現在在我的主要班級:
var aJob = new Job();
aJob.Id = 1;
aJob.Save(); //Does not work. Prototype can not see aJob.Id..
如您所見,我需要使用共享變量(例如ID)創建一個父函數,因此當我繼承父類時,可以為這些變量分配值,以便父類的共享邏輯起作用,然后擴展類就可以具體邏輯
您可以從這樣的構建開始:
var DataAccess = function() {
this.Save = function(){
console.log('DataAccess Save call', this.ListName, this.Id);
}
}
var Job = function(){
this.ListName = 'MyList';
}
Job.prototype = new DataAccess();
/**
* Delete me to use parent's Save method.
*/
Job.prototype.Save = function(){
console.log('Job Save call', this.ListName, this.Id);
}
var aJob = new Job();
aJob.Id = 1;
aJob.Save();
@stivlo在他的回答中描述了它是如何工作的: https ://stackoverflow.com/a/4778408/1127848
我遇到的問題是我想重用相同的代碼。 我想我已經以這種方式解決了問題,即時消息仍然不是100%進行原型編程的正確方法:
function DataAccess() {
//setup common variables
}
DataAccess._Save_(listname, id){
commonSaveLogic(id);
doStuff(listname);
}
function Job() {
this.ListName = 'Jobs';
DataAccess.call(this); //call DataAccess Constructor
}
Job.prototype = DataAccess;
Job.prototype.constructor = Job;
Job.ProtoType.Save = function(){
this._Save_(this.ListName, this.Id);
}
function AotherList() {
this.ListName = 'AnotherList';
DataAccess.call(this);
}
//same as above. Job and Another list both inherit off DataAccess.
不要在構造函數中使用.prototype。 我們定義.prototype以便將相同的副本共享給所有對象。
您在這里缺少很多東西。 我正在一一解釋:
首先: SaveLogic(this.Id); //doesnt work
SaveLogic(this.Id); //doesnt work
因為您不this
與函數一起使用,所以它是全局函數而不是構造函數。 而且您還沒有在任何地方定義它,因此會出現類似function SaveLogic not defined
的錯誤
為防止此錯誤,請在某處定義函數。
第二:您已傳遞this.Id
作為參數。 使用行aJob.Id = 1;
ID aJob.Id = 1;
在SaveLogic(this.Id);
中將無法訪問SaveLogic(this.Id);
因為Id是aJob的屬性,而不是ajob.prototype。 this.ListName
將在此處可用,因為它是原型的屬性。 因此,您想在SaveLogic()函數中獲取ID,並將其定義為prototype屬性。
第三:當這行aJob.Save();
將被調用它將被調用
this.Save = function(){
Job.prototype.Save().call(this);
//specific Job Save logic
}
Job.prototype.Save()將搜索一個名為Save()的函數。 Job的原型中未定義該函數,因此會發生函數未定義的錯誤。
第四:除了DataAccess.call()或Job.call()之外,都不能調用call()。
call()類似於構造函數調用,不同之處在於它的第一個參數被分配給構造函數的this對象。
在這里,我改進了您的代碼。 只需將其復制並粘貼到您的編輯器中,即可查看此處的內容。
嘗試這個 :
function SaveLogic(Id)
{
alert(Id);
}
var DataAccess = function() {
this.Save = function(){
alert(this.ListName); //works
SaveLogic(this.Id);
return this; //doesnt work
}
this.call = function() {
alert('call is called here');
}
}
var Job = function(){
Job.prototype.ListName = 'MyList'; //works
this.Save = function(){
//console.log(Job.prototype.Save());
Job.prototype.Save().call(this);
//specific Job Save logic
}
}
Job.prototype = new DataAccess();
var aJob = new Job();
Job.prototype.Id = 1;
aJob.Save(); //Does not work. Prototype can not see aJob.Id..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.