組合模式

部分-整體模式

// 寄生式繼承父類
function object(o) {    
  function F(){}    
  F.prototype=o;    
  return new F();
}
function inheritPrototype(subType,superType) {    
  var obj = object(superType.prototype);  //創(chuàng)建對(duì)象      
  obj.constructor = subType;                      //增強(qiáng)對(duì)象    
  subType.prototype = obj;                        //指定對(duì)象
}

var News = function () {    
  this.children = [];    
  this.element = null;
}
News.prototype = {    
  init : function() {        
    throw new Error('請(qǐng)重新寫你的方法');    
  },    
  add : function() {        
    throw  new Error('請(qǐng)重新寫你的方法');    
  },    
  getElement : function() {        
    throw  new Error('請(qǐng)重新寫你的方法');    
  }
}
var Container = function(id, parent) {    
  News.call(this);    
  this.id = id;    
  this.parent = parent;    
  this.init();
}
inheritPrototype(Container, News);
Container.prototype.init = function() {    
  this.element = document.createElement('ul');    
  this.element.id = this.id;    
  this.element.className = 'new-container';
};
Container.prototype.add = function(child) {    
  this.children.push(child);    
  this.element.appendChild(child.getElement());    
  return this;
}
Container.prototype.getElement = function() {    
  return this.element;
}
Container.prototype.show = function() {    
  this.parent.appendChild(this.element);
}
var Item = function(classname) {     
  News.call(this);    
  this.className = classname || '';     
  this.init();
}
inheritPrototype(Item, News);
Item.prototype.init = function() {    
  this.element = document.createElement('li');    
  this.element.className = this.className;
}
Item.prototype.add = function(child) {    
  this.children.push(child);      
  this.element.appendChild(child.getElement());    
  return this;
}
Item.prototype.getElement = function() {    
  return this.element;
}
var NewsGroup = function(classname) {    
  News.call(this);    
  this.className = classname || '';    
  this.init();
}
inheritPrototype(NewsGroup, News);
NewsGroup.prototype.init = function() {    
  this.element = document.createElement('div');    
  this.element.className = this.className;
}
NewsGroup.prototype.add = function(child) {    
  this.children.push(child);    
  this.element.appendChild(child.getElement());    
  return this;
}
NewsGroup.prototype.getElement = function() {    
  return this.element;
}
// 創(chuàng)建新聞?lì)?var ImageNews = function(url, href, classname) {    
  News.call(this);    
  this.url = url || '';    
  this.href = href || '#';    
  this.className = classname || 'normal';    
  this.init();
}
inheritPrototype(ImageNews, News);
ImageNews.prototype.init = function() {    
  this.element = document.createElement('a');    
  var img = new Image();    
  img.src = this.url;    
  this.element.appendChild(img);    
  this.element.className = 'image-news' + this.className;      
  this.element.href = this.href;
}
ImageNews.prototype.add = function() {}
ImageNews.prototype.getElement  = function () {    
  return this.element;
}
var IconNews = function(text, href, type) {    
  News.call(this);    
  this.text = text || '';    
  this.href = href || '#';    
  this.type = type || 'video';    
  this.init();
}
inheritPrototype(IconNews, News);
IconNews.prototype.init = function() {    
  this.element = document.createElement('a');    
  this.element.innerHTML = this.text;    
  this.element.href = this.href;    
  this.element.className = 'icon' + this.type;
}
IconNews.prototype.add = function () {}
IconNews.prototype.getElement = function () {    
  return this.element;
}
var EasyNews = function (text, href) {    
  News.call(this);    
  this.text = text || '';    
  this.href = href || '#';    
  this.init();
}
inheritPrototype(EasyNews, News);
EasyNews.prototype.init = function() {    
  this.element = document.createElement('a');      
  this.element.innerHTML = this.text;    
  this.element.href = this.href;    
  this.element.className = 'text';
}
EasyNews.prototype.add = function() {}
EasyNews.prototype.getElement = function () {    
  return this.element;
}
var TypeNews = function(text, href, type, pos) {    
  News.call(this);    
  this.text = text || '';    
  this.href = href || '#';    
  this.type = type || '';    
  this.pos = pos || 'left';    
  this.init();
}
inheritPrototype(TypeNews, News);
TypeNews.prototype.init = function () {    
  this.element = document.createElement('a');    
  if (this.pos === 'left') {        
    this.element.innerHTML = '[' + this.type + ']' + this.text    
  } else {        
    this.element.innerHTML = this.text + '[' + this.type + ']';    
  }    
  this.element.href = this.href;    
  this.element.className = 'text';
}
TypeNews.prototype.add = function() {}
TypeNews.prototype.getElement = function () {    
  return this.element;
}
// 創(chuàng)建新聞模塊
var news1 = new Container('news', document.body);
news1.add(    
  new  Item('normal').add(        
    new IconNews('左小祖咒最帥了', '#', 'video')    
  )
).add(    
  new Item('normal').add(        
    new IconNews('辯護(hù)人上映了', '#', 'live')    )
).add(    
  new Item('normal').add(        
    new NewsGroup('has-img').add(            
      new ImageNews('img.jpg', '#', 'small')        
    ).add(            
      new EasyNews('快速減肥法', '#')        
    ).add(            
      new EasyNews('三小時(shí)學(xué)會(huì)PHP', '#')        
    )    
  )
).add(    
  new Item('normal').add(        
    new TypeNews('擇天記', '#', '小說', 'left')    )
).add(    
  new Item('normal').add(        
    new TypeNews('大話西游', '#', '游戲', 'right')    
  )
).show();
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容