Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Javascript practices

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 285
    Answer it

    Hi folks,

    I am stuck in a javascript program while trying to call a inherit method I created.

    Any help appreciated.Thanks in advance.

    ;(function(global){
    
      var Provider = function(){
        log('creating Provider instance...');
        // A trick not to use new every time a client needs to use the API
        return new Provider.init();
      } 
    
      Provider.init = function(){
            log('init invoked...');
      }
    
      // setting prototype of init and same as prototype of provider
      Provider.init.prototype = Provider.prototype;
    
      // private variables and functions
      log = function log(msg){
        if(console && msg){
          console.log(msg);
        }
      }
    
      // setting prototype to empty object to allow creating public methods
      Provider.prototype = {
        inherit : function(_clazz, _proto){
            if(typeof _clazz !== 'undefined'){
              throw 'Unable to create instance. Classname is required.';
            }
            var self = this;
            _clazz.prototype =  function inherit(_proto){
              self.log(_clazz+' -- prototype--> '+_proto || {});
              function F(){};
              F.prototype = _proto || {};
              return new F();
            }();
        }
      };
    
      // setting alias to call using C$ from global
      global.Provider = global.C$ = Provider;
    
    }(this));


    /**************************************** TESTING ****************************************************************/

    function main(){
      function Animal(name){
      }
    
      Animal.prototype = {
        logName : function(){
          console.log('my name is '+ name);
        }
      }
    
      function Fox(name){
      }
    
      console.log('Testing prototype');
    
      C$().inherit(Fox.prototype,Animal.prototype);
    
      var animal = new Animal();
      console.log(animal.prototype);
    
      var fox = new Fox();
      console.log(fox.prototype);
    }
    
    main();

     

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: