【发布时间】:2018-04-04 20:51:14
【问题描述】:
我需要构造具有许多属性的对象。 我可以为每个属性创建一个带有一个参数的构造函数:
class Friend{
constructor(name, birthday, phone, address, job, favouriteGame, favouriteBand){
this.name = name;
this.birthday = birthday;
this.phone = phone;
this.address = address;
this.job = job;
this.favouriteGame = favouriteGame;
this.favouriteBand = favouriteBand;
}
}
或者我可以接收一个带有文字对象或包含所有值的数组的参数:
class Friend{
constructor(descriptor){
this.name = descriptor.name;
this.birthday = descriptor.birthday;
this.phone = descriptor.phone;
this.address = descriptor.address;
this.job = descriptor.job;
this.favouriteGame = descriptor.favouriteGame;
this.favouriteBand = descriptor.favouriteBand;
}
}
在什么情况下我应该使用每一个? 有没有关于这个主题的设计模式?
我对 OOP 感兴趣。我正在使用 Javascript,但它可以用任何其他支持 OOP 的语言(PHP、Java、C++、Python,任何其他可能的语言)编写
【问题讨论】:
标签: oop design-patterns signature method-signature