1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| let Factory = function (type, content) { if (this instanceof Factory) { return new this[type](content) } else { return new Factory(type, content) } } Factory.prototype = { Java: function (content) { console.log(content) }, JavaScript: function (content) { console.log(content) }, }
let data = [ { type: 'Java', content: 'This is Java', }, { type: 'JavaScript', content: 'This is JavaScript', }, ]
for (let i = 0; i < data.length; i++) Factory(data[i].type, data[i].content)
|