Js design (structural) (4) - Appearance Model

Facada skin mode

1, the appearance mode

  Exterior facade pattern, also known as model, which provides a comfortable interface provides a higher level of for a more large body of code, hides its true potential complexity. Think of this model to be presented to the developer if the simplified API, will always enhance the performance of some things.

  Whenever we use the jQuery $ (el) .css or $ (el) .animate () method, we are actually using a facade - a simpler interface to allow us to avoid public behavior in order to make it work and not call the jQuery core built-in methods do not manually. This also avoids the need to manually interact with the DOM API and maintenance of state variables.

2, examples

  We use a model to simplify the appearance of cross-browser event listener interfaces:

var addMyEvent = function( el,ev,fn ){
   if( el.addEventListener ){
            el.addEventListener( ev,fn, false );
      }else if(el.attachEvent){
            el.attachEvent( "on" + ev, fn );
      } else{
           el["on" + ev] = fn;
    }
};

  Although this is a code sample has not been optimized, but creates a common approach, it is able to provide a secure and cross-browser compatible solution.

Guess you like

Origin www.cnblogs.com/doublewhy317/p/12068950.html