Ajax::prototype 源码解读

2019-06-03 08:36:20刘景俊


/*--------------------------------------------------------------------------*/ 

/** 
 * 动态插入内容的实现,MS的Jscript实现中对象有一个 insertAdjacentHTML 方法(http: //msdn.microsoft.com/workshop/author/dhtml/reference/methods/insertadjacenthtml.asp) 
 * 这里算是一个对象形式的封装。 
 */ 
Abstract.Insertion = function(adjacency) { 
  this.adjacency = adjacency; 


Abstract.Insertion.prototype = { 
  initialize: function(element, content) { 
    this.element = $(element); 
    this.content = content; 

    if (this.adjacency && this.element.insertAdjacentHTML) { 
      this.element.insertAdjacentHTML(this.adjacency, this.content); 
    } else { 
     /** 
      * gecko 不支持 insertAdjacentHTML 方法,但可以用如下代码代替 
      */ 
      this.range = this.element.ownerDocument.createRange(); 
     /** 
      * 如果定义了 initializeRange 方法,则实行,这里相当与定义了一个抽象的 initializeRange 方法 
      */ 
      if (this.initializeRange) this.initializeRange(); 
      this.fragment = this.range.createContextualFragment(this.content); 

     /** 
      * insertContent 也是一个抽象方法,子类必须实现 
      */ 
      this.insertContent(); 
    } 
  } 


/** 
 * prototype 加深了我的体会,就是写js 如何去遵循 Don't Repeat Yourself (DRY) 原则 
 * 上文中 Abstract.Insertion 算是一个抽象类,定义了名为 initializeRange 的一个抽象方法 
 * var Insertion = new Object() 建立一个命名空间 
 * Insertion.Before|Top|Bottom|After 就象是四个java中的四个静态内部类,而它们分别继承于Abstract.Insertion,并实现了initializeRange方法。 
 */ 
var Insertion = new Object(); 

Insertion.Before = Class.create();