Advertisement
Guest User

Untitled

a guest
Dec 29th, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Extends Object with custom method for instantiating objects.
  3.  *
  4.  * @param {Object} className Object to create from.
  5.  * @param {Object|Function} params Params to the constructor method, or extended/custom constructor function.
  6.  * @param {String} constructor Name of constructor function.
  7.  * @returns {Object}
  8.  */
  9. Object.init = function(className, params, constructor)
  10. {
  11.     constructor = constructor || '_constructor';
  12.    
  13.     var Obj = Object.create(className) || {};
  14.    
  15.     if( typeof params === 'object' && typeof Obj[constructor] === 'function' )
  16.     {
  17.         Obj[constructor](params);
  18.     }
  19.     else if( typeof params === 'function' )
  20.     {
  21.         if( typeof Obj._constructor !== 'function' )
  22.         {
  23.             Obj._constructor = params;
  24.             Obj._constructor();
  25.         }
  26.         else
  27.         {
  28.             var oldConstructor = Obj._constructor;
  29.             Obj._constructor = function() {
  30.                 oldConstructor();
  31.                 params();
  32.             }
  33.         }
  34.     }
  35.    
  36.     return Obj;
  37. }
  38.  
  39. var PlaceholderRotator = {
  40.     /**
  41.      * Amount of time text is fully readable before deleting.
  42.      * @type Number
  43.      */
  44.     defaultShowTime                 : 2000,
  45.    
  46.     /**
  47.      * Amount of time between typing a character.
  48.      * @type Number
  49.      */
  50.     timeBetweenCharacterType        : 10,
  51.    
  52.     /**
  53.      * Amount of time between deleting a character.
  54.      * @type Number
  55.      */
  56.     timeBetweenCharacterDeletion    : 5,
  57.    
  58.     /**
  59.      * Constructor function.
  60.      * @param {Object} params
  61.      * @returns {undefined}
  62.      */
  63.     _constructor: function(params)
  64.     {
  65.         this.defaultShowTime                = params.defaultShowTime                || this.defaultShowTime;
  66.         this.timeBetweenCharacterType       = params.timeBetweenCharacterType       || this.timeBetweenCharacterType;
  67.         this.timeBetweenCharacterDeletion   = params.timeBetweenCharacterDeletion   || this.timeBetweenCharacterDeletion;
  68.     }
  69.    
  70. };
  71.  
  72.  
  73. // Example usages
  74.     var a = Object.init(PlaceholderRotator, {
  75.         defaultShowTime             : 1000,
  76.         timeBetweenCharacterType    : 5
  77.     });
  78.    
  79.     var b = Object.init(PlaceholderRotator, function() {
  80.         console.log('This is an extended/custom constructor method.');
  81.     });
  82.    
  83.     b.extendedFunction = function()
  84.     {
  85.         console.log('This is an extended function exclusively for b.');
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement