Compare Revisions

Inheritance revisited

Revision 56933:

Revision 56933 by Sheppy on

Revision 56934:

Revision 56934 by zegarek84 on

Title:
Inheritance revisited
Inheritance revisited
Slug:
JavaScript/Guide/Inheritance_Revisited
JavaScript/Guide/Inheritance_Revisited
Tags:
JavaScript, OOP, Inheritance
JavaScript, OOP, Inheritance
Content:

Revision 56933
Revision 56934
nn157    <h2>
158      Some alternative extend solution
159    </h2>
160    <p>
161      This solution can extend prototype don't processing parent 
 >contstructor
162    </p>
163    <h3>
164      Basic function
165    </h3>
166    <pre>
167var fProtoInstance;
168(function(){
169  var _fn = function() {}; // private constructor
170  /**
171   * Basic function for inherite prototype without parent constru
 >ctor
172   *
173   * @author Grzegorz Nowak - zegarek84 &lt;zegarek84@gmail.com&g
 >t;
174   * @function
175   * @param {Function} fn
176   * @return {Object}
177   */
178  fProtoInstance = function(fn) {
179    _fn.prototype = fn.prototype;
180    return new _fn;
181  }
182})();
183</pre>
184    <p>
185      Usage:<br>
186    </p>
187    <pre>
188function childClassConstructor(){}<br>childClassConstructor.proto
 >type = fProtoInstance(parentClassConstructor);<br>// add new meth
 >od or oweride old methode<br>childClassConstructor.prototype.newM
 >ethod = function(){};<br>
189</pre>
190    <h3>
191      Function for inherit prototype
192    </h3>
193    <pre>
194var fCloneProto;<br>(function(_f, _o){<br>  var _fn = function() 
 >{}; // private constructor<br>  /**<br>   *<br>   * @author Grzeg
 >orz Nowak - zegarek84 &lt;zegarek84@gmail.com&gt;<br>   * @functi
 >on<br>   * @param {Function|Object} fn<br>   * @return {Object|fa
 >lse}<br>   */<br>  fCloneProto = function(fn) {<br>    var _myFn 
 >= _fn, oCloned = false;<br>    if (fn instanceof _f) {<br>      _
 >myFn.prototype = fn.prototype;<br>      _myFn.prototype['_parent'
 >] = fn<br>      oCloned = new _myFn;<br>    } else if (fn instanc
 >eof _o) {<br>      _myFn.prototype = fn;<br>      oCloned = new _
 >myFn;<br>    }<br>    _myFn = null;<br>    return oCloned;<br>  }
 ><br>})(Function, Object);<br>
195</pre>
196    <p>
197      &nbsp;
198    </p>
tt205    <p>
206      <br>
207    </p>

Back to History