var page = page || {};
page.dom = page.dom || {};
//子函數1:處理文本
page.dom.Text = function () {
this.insert = function (where) {
var txt = document.createTextNode(this.url);
where.appendChild(txt);
};
};
//子函數2:處理鏈接
page.dom.Link = function () {
this.insert = function (where) {
var link = document.createElement('a');
link.href = this.url;
link.appendChild(document.createTextNode(this.url));
where.appendChild(link);
};
};
//子函數3:處理圖片
page.dom.Image = function () {
this.insert = function (where) {
var im = document.createElement('img');
im.src = this.url;
where.appendChild(im);
};
};
工廠方法接口
[javascript]
page.dom.factory = function (type) {
return new page.dom[type];
}
調用方式
[javascript]
var o = page.dom.factory('Link');
o.url = 'https://www.csdn.com';
o.insert(document.body);
摘自 PainsOnline的專欄