7878
7979console . log ( clark instanceof Man ) ; // true
8080console . log ( clark instanceof SuperMan ) ; // true
81+ < ! doctype html >
82+ < html lang = "en" >
83+ < head >
84+ < title > JavaScript Patterns</ title >
85+ < meta charset = "utf-8" >
86+ </ head >
87+ < body >
88+ < script >
89+ /* Title: Classical Pattern #5 - A Temporary Constructor (a pattern that should be generally avoided)
90+ Description: first borrow the constructor and then also set the child's prototype to point to a new instance of the constructor
91+ */
92+
93+ /* Basic */
94+ /*function inherit(C, P) {
95+ var F = function ( ) { } ;
96+ F . prototype = P . prototype ;
97+ C . prototype = new F ( ) ;
98+ } */
99+
100+ /* Storing the Superclass */
101+ /*function inherit(C, P) {
102+ var F = function ( ) { } ;
103+ F . prototype = P . prototype ;
104+ C . prototype = new F ( ) ;
105+ C . uber = P . prototype ;
106+ } */
107+
108+ /* Resetting the Constructor Pointer */
109+ /*function inherit(C, P) {
110+ var F = function ( ) { } ;
111+ F . prototype = P . prototype ;
112+ C . prototype = new F ( ) ;
113+ C . uber = P . prototype ;
114+ C . prototype . constructor = C ;
115+ } */
116+
117+ /* in closure */
118+ var inherit = (function () {
119+ var F = function ( ) { } ;
120+ return function ( C , P ) {
121+ F . prototype = P . prototype ;
122+ C . prototype = new F ( ) ;
123+ C . uber = P . prototype ;
124+ C . prototype . constructor = C ;
125+ }
126+ } ());
127+
128+ function Parent(name) {
129+ this . name = name || 'Adam' ;
130+ }
131+
132+ // adding functionality to the prototype
133+ Parent.prototype.say = function () {
134+ return this . name ;
135+ } ;
136+
137+ // child constructor
138+ function Child(name) { }
139+
140+ inherit(Child, Parent);
141+
142+ var kid = new Child();
143+ console.log(kid.name); // undefined
144+ console.log(typeof kid.say); // function
145+ kid.name = 'Patrick';
146+ console.log(kid.say()); // Patrick
147+ console.log(kid.constructor.name); // Child
148+ console.log(kid.constructor === Parent); // false
149+
150+
151+ // reference
152+ // http://shop.oreilly.com/product/9780596806767.do
153+ </ script >
154+ </ body >
155+ </ html >
81156</ script>
82157</ body>
83158</ html>
0 commit comments