Search code examples
javascriptinheritanceiife

Javascript object inheritence with iife


I am working with using an IIFE for an HTML5/CSS3/JS web widget game. For it, I want to create specialized exception objects that are descendants of a single object. Am I attempting the impossible?

The code so far is

window.Exception = (function(t) {
  Exception.title = t;
  Exception.msg = '';
  function Exception(message) { this.msg = message; }
  Exception.prototype = {
    toString: function() { return this.title + ': ' + this.msg; }
  };
  return Exception;
})('Exception');

window.RefException = (function(parent, t) {
  RefException.title = t;
  RefException.msg = '';
  function RefException(message) { this.msg = message; }
  RefException.prototype = parent.prototype;
  return RefException;
})(window.Exception, 'Reference Exception');

console.log((new RefException('blah')).toString());

And when I look at the console, I get undefined: blah, so RefException is inheriting the prototype functions, but not the namespaced variable title. Am I missing something?


Solution

  • It seems I found the problem, There are 2 logical error in your codes:
    Some notes:
    1. JavaScript has a property named Hoisting, It means every variable or function declaration in block is hoisting to the top of block by JS.
    2. In JS functions is also Objects

    Now lets get start with a snippet of your code:

    window.Exception = (function(t) {
      Exception.title = t;
      Exception.msg = '';
      function Exception(message) { this.msg = message; }
      Exception.prototype = {
        toString: function() { return this.title + ': ' + this.msg; }
      };
      return Exception;
    })('Exception');
    

    This snippet is equals to:

    window.Exception = (function(t) {
          function Exception(message) { this.msg = message; } // <-- Hoisted by JS
          Exception.title = t;
          Exception.msg = '';
          Exception.prototype = {
            toString: function() { return this.title + ': ' + this.msg; }
          };
          return Exception;
        })('Exception');
    

    Now let analyze this snippet code:
    - You create an Object (not class) of type function named Exception
    - You've added some properties to this object(like other literal objects) named title and msg
    - You have assigned a literal object to Exception.prototype
    - And finally return the Object Exception which is a simple function with some additional properties.

    Now lets do some test with this function:

    var obj = new Exception();
    

    What happens with this statements?

    1. Function Exception acts as a constructor function
    2. A new object will be created and assigned to this
    3. A new property named msg will be added to this newly created object and value of message variable will be assigned to it
    4. And this object will be returned from this function and assigned to obj

    Now, can you find what is the problem??
    the obj object does not have any property named title
    Note that title is property of Exception object NOT property of objects created by this constructor function

    Hope this helps you!