使用构造函数

时间:2017-10-18 17:23:37

标签: javascript

我正在创建一个包含表单状态的组件,但是我希望这个组件成为多个表单的控制器,所以我可以使用该表单来创建和编辑,它看​​起来有些难看现在:

formController.js

state = { 
  //first form rental
    id: ''
    cost: 0,
    description: '',
    address: ''
  //second form rent
    id:'',
    price:0,
    tenant:'',
    paymentDate:
}

所以我想知道如果为每个表单创建一个对象构造函数是否有好的方法,这就是我现在按照OOP样式创建状态的方法:

constructors.js:

export const Rental = (cod, cost, address, description) => {
    return {
        cod,
        cost,
        address,
        description
    }
}
export const Rent = (cod, price, tenant, paymentDate) => {
    return {
        cod,
        price,
        tenant,
        paymentDate,
    }
}

formController.js:

state = { 
       newRental: new Rental('', 0, '', ''),
       newRent: new Rent('', 0, '', '')
    }

对我来说看起来好多了,这是处理自定义数据类型的好方法吗?

0 个答案:

没有答案
相关问题