1

I am trying to use RMP for most of my modules styles. However, I want to understand it completely with few questions below.

  1. Object.create() seems working for RMP module inheritance. Am I missing something?

    var Obj = (function(){
        //do some constructor stuff
    })();
    
    var InheritingObj = (function(){
        //do some constructor stuff
    })();
    
    InheritingObj.prototype = Object.create(Obj.prototype);
    
  2. Define prototype and return to Obj works too Do I understand something wrong? It seems like prototype inheritance is working for RMP.

user2734550
  • 952
  • 1
  • 12
  • 35
  • Depends on what your RMP modules are. Are they actual classes (constructors) or singleton objects? Your `Obj` doesn't look like a constructor, so inheritance doesn't make much sense there. – Bergi Feb 17 '15 at 20:58
  • possible duplicate of either [Inheritance and module pattern](http://stackoverflow.com/a/15437040/1048572) or [How to implement inheritance in JS Revealing prototype pattern?](http://stackoverflow.com/q/9248655/1048572) (or [Creating inheritance on revealing modular pattern objects](http://stackoverflow.com/q/18817218/1048572) for singletons) – Bergi Feb 17 '15 at 21:01
  • @Bergi, here's a better example. http://jsfiddle.net/j3u7jnan/ – user2734550 Feb 17 '15 at 23:06
  • That example doesn't make much sense actually. Are you trying to use `Object.create` for creating instances? If so, I had expected [something like this](http://jsfiddle.net/j3u7jnan/1/) – Bergi Feb 17 '15 at 23:13
  • Nothing in your code indicates use of Revealing Module Pattern. Am I missing something? – I-Lin Kuo Feb 18 '15 at 00:08
  • @I-LinKuo: Ok, le'ts [reveal `sam`](http://jsfiddle.net/j3u7jnan/2/) if you care for the looks of the pattern :-) You can usually mutate those module IEFEs quite freely without changing their meaning. – Bergi Feb 18 '15 at 00:26
  • @Bergi, your latest example above and my 2nd example http://jsfiddle.net/d0n7kfmx/ in the question confirms that we can inherit from RMP. am I right? It's just yours is not prototype inheritance, mine is? – user2734550 Feb 18 '15 at 02:11
  • 1
    @Bergi, I should have clarified my remark was directed toward OP. Nothing in the OP's posted code has anything to do with Revealing Module Pattern. OP, using RMP doesn't prevent you from doing prototypal inheritance by throwing an error. Your fiddle isn't RMP -- RMP creates everything in the closure first and then references only the closure, and then reveals in the returned object literal. See http://stackoverflow.com/questions/22906662/javascript-design-pattern-difference-between-module-pattern-and-revealing-modul/22918556#22918556 – I-Lin Kuo Feb 18 '15 at 10:00
  • @user2734550: No, you inherit from an object or from a constructor ("class"). How that was constructed, or how your inheriting object is constructed, doesn't matter for inheritance; you don't inherit *from RMP*. – Bergi Feb 18 '15 at 13:46
  • @I-LinKuo: OK, interesting distinction. Sorry I confused your comment with a response from OP. – Bergi Feb 18 '15 at 13:47

0 Answers0