Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中构造函数中的构造函数?_Javascript_Object_Constructor - Fatal编程技术网

javascript中构造函数中的构造函数?

javascript中构造函数中的构造函数?,javascript,object,constructor,Javascript,Object,Constructor,我想要一个构造函数中的构造函数-我已经搜索了stackoverflow并在Google上搜索了很多 我有一个构造函数RentalProperty: function RentalProperty(numberOfUnits, address, dateAcquired, acCost, isFinanced, loanAmount){ this.numberOfUnits = numberOfUnits; this.address = address; this.

我想要一个构造函数中的构造函数-我已经搜索了stackoverflow并在Google上搜索了很多

我有一个构造函数
RentalProperty

function RentalProperty(numberOfUnits, address, dateAcquired, acCost, isFinanced,                
loanAmount){
this.numberOfUnits = numberOfUnits;
this.address = address;
this.dateAcquired = new Date(dateAcquired);
this.acCost = acCost;
this.isFinanced = isFinanced; 
this.loanAmount = 0;   
this.newValue = 0;
this.equity = function(){
    if (this.newValue === 0){
        return (this.acCost - this.loanAmount);
    } else {
        return (this.newValue - this.loanAmount);
    }
 };
}
RentalProperty.prototype.unit1 = {
unitNumber : "1",
monthlyRent: 0,
leaseStart: new Date(0),
leaseEnd: new Date(0),
numBeds: 0, 
isSec8: false,
tenantPortion: 0,
sec8Portion: 0
};

RentalProperty.prototype.unit2 = {
unitNumber : "2",
monthlyRent: 0,
leaseStart: new Date(0),
leaseEnd: new Date(0),
numBeds: 0,
isSec8: false,
tenantPortion: 0,
sec8Portion: 0
};

RentalProperty.prototype.unit3 = {
unitNumber : "3",
monthlyRent: 0,
leaseStart: new Date(0),
leaseEnd: new Date(0),
numBeds: 0,
isSec8: false,
tenantPortion: 0,
sec8Portion: 0
};
RentalProperty.Unit = function(unitNumber) {
    this.unitNumber = unitNumber;
}
RentalProperty
的每个实例都将有一系列的
unit
对象,我希望这些对象是
unit1
unit2
unit3
,等等。但是
RentalProperty
的一些实例将只有一个
单元,而其他实例可能有六个、十二个或更多。我在这里做的方式似乎不正确,因为代码重复很多,我需要制作大量的
unit
对象,这些对象可能不用于
RentalProperty
的特定实例:

function RentalProperty(numberOfUnits, address, dateAcquired, acCost, isFinanced,                
loanAmount){
this.numberOfUnits = numberOfUnits;
this.address = address;
this.dateAcquired = new Date(dateAcquired);
this.acCost = acCost;
this.isFinanced = isFinanced; 
this.loanAmount = 0;   
this.newValue = 0;
this.equity = function(){
    if (this.newValue === 0){
        return (this.acCost - this.loanAmount);
    } else {
        return (this.newValue - this.loanAmount);
    }
 };
}
RentalProperty.prototype.unit1 = {
unitNumber : "1",
monthlyRent: 0,
leaseStart: new Date(0),
leaseEnd: new Date(0),
numBeds: 0, 
isSec8: false,
tenantPortion: 0,
sec8Portion: 0
};

RentalProperty.prototype.unit2 = {
unitNumber : "2",
monthlyRent: 0,
leaseStart: new Date(0),
leaseEnd: new Date(0),
numBeds: 0,
isSec8: false,
tenantPortion: 0,
sec8Portion: 0
};

RentalProperty.prototype.unit3 = {
unitNumber : "3",
monthlyRent: 0,
leaseStart: new Date(0),
leaseEnd: new Date(0),
numBeds: 0,
isSec8: false,
tenantPortion: 0,
sec8Portion: 0
};
RentalProperty.Unit = function(unitNumber) {
    this.unitNumber = unitNumber;
}
我尝试了各种语法组合(我已经花了好几个小时的时间)在
RentalProperty
构造函数中放置
unit
构造函数,代码如下:

....
this.unit["for(i=0, i < this.numberOfUnits, i++){return i;}"] = {
    unitNumber : "i",
    monthlyRent: 0,
    leaseStart: new Date(0),
    leaseEnd: new Date(0),
    numBeds: 0, 
    isSec8: false,
    tenantPortion: 0,
    sec8Portion: 0
    };
…但当我尝试创建一个新的
Units
类的实例时,在它前面有
RentalProperty
实例,它用点表示法表示“意外令牌”

我只学了两个月的代码(html和javascript各大约一个月),所以我很确定这是一个noob问题。任何帮助都将不胜感激。。。。。这也是我的第一个stackoverflow问题,因此如果我的格式设置被关闭,请接受我的道歉。

似乎您想要

this.units = []; // an array which
for (var i=0; i < this.numberOfUnits; i++) { // in a loop
    this.units[i] = { // is filled with objects
        unitNumber : i,
        monthlyRent: 0,
        leaseStart: new Date(0),
        leaseEnd: new Date(0),
        numBeds: 0, 
        isSec8: false,
        tenantPortion: 0,
        sec8Portion: 0
    };
}
好像你想要

this.units = []; // an array which
for (var i=0; i < this.numberOfUnits; i++) { // in a loop
    this.units[i] = { // is filled with objects
        unitNumber : i,
        monthlyRent: 0,
        leaseStart: new Date(0),
        leaseEnd: new Date(0),
        numBeds: 0, 
        isSec8: false,
        tenantPortion: 0,
        sec8Portion: 0
    };
}

只考虑一个完全独立的单元< /P>构造函数 我把它作为

RentalProperty
的一个属性来保持一切整洁。
接下来,在RentalProperty构造函数中生成所有单位,或者

this.units = [];
var i;
for (i = 0; i < this.numberOfUnits; ++i) {
    this.units.push(new RentalProperty.Unit(i));
}
this.units=[];
var i;
对于(i=0;i

这样做也意味着你可以为你的单位建立一个原型链,如果你想要的话,你可以确认一个单元<代码> u>代码>确实是一个单位,使用<代码> u的租金属性。单元< /代码> .< /p>

只考虑一个完全独立的单位< /P>构造函数 我把它作为

RentalProperty
的一个属性来保持一切整洁。
接下来,在RentalProperty构造函数中生成所有单位,或者

this.units = [];
var i;
for (i = 0; i < this.numberOfUnits; ++i) {
    this.units.push(new RentalProperty.Unit(i));
}
this.units=[];
var i;
对于(i=0;i

这样做还意味着,如果您愿意,您可以为Unit设置原型链,并且您可以使用RentalProperty的
u实例来确认Unit
u
确实是一个Unit。Unit

您正在过度复杂化事物。只要已经定义了
单元
构造函数,您就可以执行以下操作:

function RentalProperty(numberOfUnits){
    this.numberOfUnits = numberOfUnits;
    this.units = [];
    for (var i = 0; i < numberOfUnits; ++i) {
       this.units.push(new Unit(i));
    }
}
或者也可以将其设置为
RentalProperty
的属性:

function RentalProperty(numberOfUnits, address, dateAcquired, acCost, isFinanced,                
loanAmount){
this.numberOfUnits = numberOfUnits;
this.address = address;
this.dateAcquired = new Date(dateAcquired);
this.acCost = acCost;
this.isFinanced = isFinanced; 
this.loanAmount = 0;   
this.newValue = 0;
this.equity = function(){
    if (this.newValue === 0){
        return (this.acCost - this.loanAmount);
    } else {
        return (this.newValue - this.loanAmount);
    }
 };
}
RentalProperty.prototype.unit1 = {
unitNumber : "1",
monthlyRent: 0,
leaseStart: new Date(0),
leaseEnd: new Date(0),
numBeds: 0, 
isSec8: false,
tenantPortion: 0,
sec8Portion: 0
};

RentalProperty.prototype.unit2 = {
unitNumber : "2",
monthlyRent: 0,
leaseStart: new Date(0),
leaseEnd: new Date(0),
numBeds: 0,
isSec8: false,
tenantPortion: 0,
sec8Portion: 0
};

RentalProperty.prototype.unit3 = {
unitNumber : "3",
monthlyRent: 0,
leaseStart: new Date(0),
leaseEnd: new Date(0),
numBeds: 0,
isSec8: false,
tenantPortion: 0,
sec8Portion: 0
};
RentalProperty.Unit = function(unitNumber) {
    this.unitNumber = unitNumber;
}

在这种情况下,您将使用
新建RentalProperty.Unit(i)

创建单元,这将使事情变得过于复杂。只要已经定义了
单元
构造函数,您就可以执行以下操作:

function RentalProperty(numberOfUnits){
    this.numberOfUnits = numberOfUnits;
    this.units = [];
    for (var i = 0; i < numberOfUnits; ++i) {
       this.units.push(new Unit(i));
    }
}
或者也可以将其设置为
RentalProperty
的属性:

function RentalProperty(numberOfUnits, address, dateAcquired, acCost, isFinanced,                
loanAmount){
this.numberOfUnits = numberOfUnits;
this.address = address;
this.dateAcquired = new Date(dateAcquired);
this.acCost = acCost;
this.isFinanced = isFinanced; 
this.loanAmount = 0;   
this.newValue = 0;
this.equity = function(){
    if (this.newValue === 0){
        return (this.acCost - this.loanAmount);
    } else {
        return (this.newValue - this.loanAmount);
    }
 };
}
RentalProperty.prototype.unit1 = {
unitNumber : "1",
monthlyRent: 0,
leaseStart: new Date(0),
leaseEnd: new Date(0),
numBeds: 0, 
isSec8: false,
tenantPortion: 0,
sec8Portion: 0
};

RentalProperty.prototype.unit2 = {
unitNumber : "2",
monthlyRent: 0,
leaseStart: new Date(0),
leaseEnd: new Date(0),
numBeds: 0,
isSec8: false,
tenantPortion: 0,
sec8Portion: 0
};

RentalProperty.prototype.unit3 = {
unitNumber : "3",
monthlyRent: 0,
leaseStart: new Date(0),
leaseEnd: new Date(0),
numBeds: 0,
isSec8: false,
tenantPortion: 0,
sec8Portion: 0
};
RentalProperty.Unit = function(unitNumber) {
    this.unitNumber = unitNumber;
}

在这种情况下,您将使用
新建RentalProperty.Unit(i)

创建单元。问题已经回答了好几次,但下面是完整的代码

function Unit(rentProp){
  this.unitNumber = ""+(rentProp.units.length+1);
  this.rentalProperty=rentProp
  //all of the foolowing props could go on prototype
  //because the constructor fills them only with default values
  //later you'll assign new values to them with
  //someUnitInstance.monthlyRent=... 
  this.monthlyRent = 0;
  this.leaseStart = null;
  this.leaseEnd = null;
  this.numBeds = 0;
  this.isSec8 = false;
  this.tenantPortion = 0;
  this.sec8Portion = 0;
}

function RentalProperty(numberOfUnits, address
 , dateAcquired, acCost, isFinanced
 ,loanAmount){
  this.numberOfUnits = numberOfUnits;
  this.address = address;
  this.dateAcquired = new Date(dateAcquired);
  this.acCost = acCost;
  this.isFinanced = isFinanced;
  this.units=[];
};
//default values on prototype, assuming you're not using
//hasOwnProperty later to iterate through properties because
//defaults (on prototype) won't show up
RentalProperty.prototype.loanAmount = 0;   
RentalProperty.prototype.newValue = 0;
RentalProperty.prototype.equity = function(){
  if (this.newValue === 0){
    return (this.acCost - this.loanAmount);
  } else {
    return (this.newValue - this.loanAmount);
  }
};
RentalProperty.prototype.addUnit = function(){
  this.units.push(new Unit(this));
}

var r=new RentalProperty();
r.addUnit();
r.addUnit();
console.log(r.units);

这个问题已经被回答了好几次,但下面是完整的代码

function Unit(rentProp){
  this.unitNumber = ""+(rentProp.units.length+1);
  this.rentalProperty=rentProp
  //all of the foolowing props could go on prototype
  //because the constructor fills them only with default values
  //later you'll assign new values to them with
  //someUnitInstance.monthlyRent=... 
  this.monthlyRent = 0;
  this.leaseStart = null;
  this.leaseEnd = null;
  this.numBeds = 0;
  this.isSec8 = false;
  this.tenantPortion = 0;
  this.sec8Portion = 0;
}

function RentalProperty(numberOfUnits, address
 , dateAcquired, acCost, isFinanced
 ,loanAmount){
  this.numberOfUnits = numberOfUnits;
  this.address = address;
  this.dateAcquired = new Date(dateAcquired);
  this.acCost = acCost;
  this.isFinanced = isFinanced;
  this.units=[];
};
//default values on prototype, assuming you're not using
//hasOwnProperty later to iterate through properties because
//defaults (on prototype) won't show up
RentalProperty.prototype.loanAmount = 0;   
RentalProperty.prototype.newValue = 0;
RentalProperty.prototype.equity = function(){
  if (this.newValue === 0){
    return (this.acCost - this.loanAmount);
  } else {
    return (this.newValue - this.loanAmount);
  }
};
RentalProperty.prototype.addUnit = function(){
  this.units.push(new Unit(this));
}

var r=new RentalProperty();
r.addUnit();
r.addUnit();
console.log(r.units);

它到底在哪里说“意外令牌”?Bergi-它在我的IDE结果窗格中说“意外令牌”(lol-我是这样一个noob,我不知道这是不是正确的术语!)我的意思是“什么行,什么代码?”而不是“在哪个屏幕上”:-)奇怪的是,你将特定于实例的成员放在原型上,但不是特定于实例的成员上(衡平法)在构造函数主体中。应该是相反的。正如其他人回答的那样,单元成员应该是一个单元数组(this.units),您可以定义
Rentalproperty.prototype.addUnit、removeUnit、findUnit…
函数来操作它们。这里有更多关于原型的信息:它到底在哪里说“意外令牌”?Bergi-它在我的IDE结果窗格中说“意外令牌”(lol-我是这样一个noob,我不知道这是否是正确的术语!)我的意思是“什么行,什么代码?”而不是“在哪个屏幕上”:-)奇怪的是,您将实例特定的成员放在原型上,而将非实例特定的成员(equity方法)放在构造函数体中。应该是相反的。正如其他人回答的那样,单元成员应该是一个单元数组(this.units),您可以定义
Rentalproperty.prototype.addUnit、removeUnit、findUnit…
函数来操作它们。有关原型的更多信息,请参见: