var Factory = function (type, content) {
if (this instanceof Factory) {
var s = new this[type](content);
return s;
} else {
return new Factory(type, content);
}
}
Factory.prototype = {
Java: function (content) {
},
UI: function (content) {
},
PHP: function (content) {
},
JavaScript: function (content) {
this.content = content;
(function (content) {
var div = document.createElement('div');
div.innerHTML = content;
div.style.border = '1px solid red';
document.getElementById('container').appendChild(div);
})(content);
},
}
var data = [
{type: 'JavaScript',content: 'JavaScript哪家强'},
{type: 'Java',content: 'Java哪家强'},
{type: 'UI',content: 'UI哪家强'},
{type: 'PHP',content: 'PHP哪家强'}
];
console.log(data);
for (var i = 3; i >= 0; i--) {
Factory(data[i].type, data[i].content);
}
完整html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
var Factory = function(type, content) {
if (this instanceof Factory) {
var s = new this[type](content);
return s;
} else {
return new Factory(type, content);
}
}
Factory.prototype = {
Java: function(content) {
},
UI: function(content) {
},
PHP: function(content) {
},
JavaScript: function(content) {
this.content = content;
(function(content) {
var div = document.createElement('div');
div.innerHTML = content;
div.style.border = '1px solid red';
document.getElementById('container').appendChild(div);
})(content);
},
}
var data = [
{ type: 'JavaScript', content: 'JavaScript哪家强' },
{ type: 'Java', content: 'Java哪家强' },
{ type: 'UI', content: 'UI哪家强' },
{ type: 'PHP', content: 'PHP哪家强' }
];
console.log(data);
for (var i = 3; i >= 0; i--) {
Factory(data[i].type, data[i].content);
}
</script>
</body>
</html>
如图:
