动态实例化传递参数
Dynamic instantiations passing parameters
我 运行 遇到了从不同的 class 动态生成对象实例的问题。所以我有一个返回产品数组的服务器响应,这些产品的类型各不相同,这将因要使用的实例化 class 以及传递给构造函数的参数而异。
在下面的示例中,我有一个根 class Product
,它由 class Food
和 classFurniture
扩展,这些扩展 class 的参数各不相同,需要插入他们的亲戚constructors()
。
我正在使用产品 属性 product.metaType
来保存有关 class
在实例化中使用的信息。
//root product class which all inherit from
class Product{
constructor(price){
this.price = price;
this.quantity = null;
}
}
//food product class
class Food extends Product{
constructor(price,exp){
super(price);
this.expiration = exp;
}
}
//furniture product class
class Furniture extends Product{
constructor(price,dimensions){
this.price = price;
this.dimensions = dimensions;
}
}
class Chair extends Furniture{
constructor(price,dimensions,type){
super(price,dimensions);
this.type = type;
}
}
class Pizza extends Food{
constructor(price,exp,type){
super(price,exp);
this.type = type;
}
}
class Cookies extends Food{
constructor(price,exp,type){
super(price,exp);
this.type = type;
}
}
class IceCream extends Food{
constructor(price,exp,type){
super(price,exp);
this.type = type;
}
}
//init (iife)
(()=>{
//list of products emulated from a server response
var products:[
{
metaType: "Pizza",
name: "digiorno",
price: 20,
type: "pepperoni",
exp: new Date()
},
{
metaType: "Chair",
name: "Lazy Boy",
price: 400,
dimensions:{
height: 4,
width: 2,
length: 6
},
type: "modern"
},
{
metaType: "Cookies",
name: "Mrs. Fields",
price: 10,
type: "chocolate chip",
exp: new Date()
},
{
metaType: "IceCream",
name: "Ben & Jerry's",
price: 15,
type: "half baked",
exp: new Date()
}
];
var storeProducts = [];
//loop through products array
products.forEach((item)=>{
//create new instance of the specific product, append to the 'storeProducts' array
//each product contains a metaType property which is the CLASS to use
//how can I dynamically do this?
storeProducts.push(new window[item.metaType]());
});
})();
问题:如何动态实例化不同的class并传递参数?
创建 name -> constructor
地图。 class
es 不会成为全局对象的属性:
const cls = {Food, Furniture, ...};
让构造函数接受一个参数并解构它,例如
class Food extends Product{
constructor({price,exp}){
// ^ ^ destructuring
super(price);
this.expiration = exp;
}
}
然后你可以将整个对象传递给构造函数:
new cls[item.metaType](item);
我 运行 遇到了从不同的 class 动态生成对象实例的问题。所以我有一个返回产品数组的服务器响应,这些产品的类型各不相同,这将因要使用的实例化 class 以及传递给构造函数的参数而异。
在下面的示例中,我有一个根 class Product
,它由 class Food
和 classFurniture
扩展,这些扩展 class 的参数各不相同,需要插入他们的亲戚constructors()
。
我正在使用产品 属性 product.metaType
来保存有关 class
在实例化中使用的信息。
//root product class which all inherit from
class Product{
constructor(price){
this.price = price;
this.quantity = null;
}
}
//food product class
class Food extends Product{
constructor(price,exp){
super(price);
this.expiration = exp;
}
}
//furniture product class
class Furniture extends Product{
constructor(price,dimensions){
this.price = price;
this.dimensions = dimensions;
}
}
class Chair extends Furniture{
constructor(price,dimensions,type){
super(price,dimensions);
this.type = type;
}
}
class Pizza extends Food{
constructor(price,exp,type){
super(price,exp);
this.type = type;
}
}
class Cookies extends Food{
constructor(price,exp,type){
super(price,exp);
this.type = type;
}
}
class IceCream extends Food{
constructor(price,exp,type){
super(price,exp);
this.type = type;
}
}
//init (iife)
(()=>{
//list of products emulated from a server response
var products:[
{
metaType: "Pizza",
name: "digiorno",
price: 20,
type: "pepperoni",
exp: new Date()
},
{
metaType: "Chair",
name: "Lazy Boy",
price: 400,
dimensions:{
height: 4,
width: 2,
length: 6
},
type: "modern"
},
{
metaType: "Cookies",
name: "Mrs. Fields",
price: 10,
type: "chocolate chip",
exp: new Date()
},
{
metaType: "IceCream",
name: "Ben & Jerry's",
price: 15,
type: "half baked",
exp: new Date()
}
];
var storeProducts = [];
//loop through products array
products.forEach((item)=>{
//create new instance of the specific product, append to the 'storeProducts' array
//each product contains a metaType property which is the CLASS to use
//how can I dynamically do this?
storeProducts.push(new window[item.metaType]());
});
})();
问题:如何动态实例化不同的class并传递参数?
创建 name -> constructor
地图。 class
es 不会成为全局对象的属性:
const cls = {Food, Furniture, ...};
让构造函数接受一个参数并解构它,例如
class Food extends Product{
constructor({price,exp}){
// ^ ^ destructuring
super(price);
this.expiration = exp;
}
}
然后你可以将整个对象传递给构造函数:
new cls[item.metaType](item);