【问题标题】:constructor within a constructor in javascript?javascript中的构造函数中的构造函数?
【发布时间】:2013-08-27 19:21:34
【问题描述】:

我想要一个构造函数中的构造函数——我已经搜索了 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 的每个实例将有一系列 unit 对象,我想成为 unit1unit2unit3 等。但是 RentalProperty 的某些实例将只有一个 @ 987654330@ 而其他人可能有六个、十二个或更多。我在这里做的方式似乎不对,因为有很多重复的代码,我需要制作大量的 unit 对象,这些对象可能不会用于 RentalProperty 的特定实例:

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 构造函数,代码如下:

....
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
    };

....希望这将使用RentalPropertythis.numOfUnits 属性的值创建正确数量的units,但它给了我“缺少操作数”。

我也试过了:

....//'new Object' added
this.unit[for(i=0, i < this.numberOfUnits, i++){return i;}] = new Object{
    unitNumber : "i",
    monthlyRent: 0,
    leaseStart: new Date(0),
    leaseEnd: new Date(0),
    numBeds: 0, 
    isSec8: false,
    tenantPortion: 0,
    sec8Portion: 0
    };
....

....//trying to make another constructor
function Units(){
unitNumber = "1";
monthlyRent = 0;
leaseStart = new Date(0);
leaseEnd = new Date(0);
numBeds = 0;
isSec8 = false;
tenantPortion = 0;
sec8Portion = 0;
}

var yale = new RentalProperty()

var yale.unit33 = new Units(); 

....但是当我尝试创建一个新的 Units 类的实例时,它前面的 RentalProperty 实例以点表示法显示为“意外令牌”。

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

【问题讨论】:

  • 它到底在哪里说“意外的令牌”?
  • Bergi- 它在我的 IDE 结果窗格中显示“意外令牌”(大声笑- 我是个菜鸟,我不知道这是否是正确的术语!)
  • 我的意思是“哪一行,什么代码?”而不是“在哪个屏幕上”:-)
  • 奇怪的是,您将实例特定成员放在原型上,但将非实例特定成员(股权方法)放在构造函数主体中。应该反过来。正如其他人回答的那样,单元成员应该是一个单元数组(this.units),您可以定义Rentalproperty.prototype.addUnit, removeUnit, findUnit ... 函数来操作它们。更多关于原型的信息:stackoverflow.com/questions/16063394/…

标签: javascript object constructor


【解决方案1】:

好像你想要的

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
    };
}

当然,您也可以使用new Units(i) 或其他东西来代替对象字面量来创建单元对象。

如果您不想要一个数组,但在您的对象上创建编号属性(我不鼓励这样做),那就是

this["unit"+i] = …

【讨论】:

    【解决方案2】:

    你把事情复杂化了。只要已经定义了Unit 构造函数,您就可以这样做:

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

    这个构造函数可以是全局范围的,如

    function Unit(unitNumber) {
        this.unitNumber = unitNumber;
    }
    

    或者您也可以将其设为RentalProperty 的属性:

    RentalProperty.Unit = function(unitNumber) {
        this.unitNumber = unitNumber;
    }
    

    在这种情况下,您将使用new RentalProperty.Unit(i) 创建单位。

    【讨论】:

      【解决方案3】:

      只需考虑一个完全独立的 Unit

      构造函数
      RentalProperty.Unit = function Unit(i) {
          if (undefined === i) i = 1;
          this.unitNumber = '' + i;
          this.monthlyRent = 0;
          this.leaseStart = new Date(0);
          this.leaseEnd = new Date(0);
          this.numBeds = 0;
          this.isSec8 = false;
          this.tenantPortion = 0;
          this.sec8Portion = 0;
      };
      

      我把它作为RentalProperty 的属性来保持一切整洁。
      接下来在 RentalProperty 构造函数中生成所有单元,或者......

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

      这样做也意味着您可以为 Unit 设置一个 prototype 链,如果您愿意的话,您可以确认一个单元 u 确实是单位使用u instanceof RentalProperty.Unit

      【讨论】:

        【解决方案4】:

        这个问题已经回答了好几次了,这里是完整的代码。

        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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-30
          • 1970-01-01
          • 2018-06-09
          • 2013-08-26
          • 2021-01-08
          • 2023-03-07
          • 2011-05-13
          相关资源
          最近更新 更多