1+ "use strict" ;
2+
3+ // Polyfill -- Only necessary for browsers which don't support Object.create. Check this ES5 compatibility table:
4+ // http://kangax.github.com/es5-compat-table/
5+ if ( ! Object . create ) {
6+ Object . create = function ( o ) {
7+ if ( arguments . length > 1 ) {
8+ throw new Error ( 'Object.create implementation only accepts the first parameter.' ) ;
9+ }
10+ function F ( ) { }
11+ F . prototype = o ;
12+ return new F ( ) ;
13+ } ;
14+ }
15+
16+
17+ // Credit to Yehuda Katz for `fromPrototype` function
18+ // http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/
19+ var fromPrototype = function ( prototype , object ) {
20+ var newObject = Object . create ( prototype ) ;
21+ for ( var prop in object ) {
22+ if ( object . hasOwnProperty ( prop ) ) {
23+ newObject [ prop ] = object [ prop ] ;
24+ }
25+ }
26+ return newObject ;
27+ } ;
28+
29+ // Define the Pizza product
30+ var Pizza = {
31+ description : 'Plain Generic Pizza'
32+ } ;
33+
34+ // And the basic PizzaStore
35+ var PizzaStore = {
36+ createPizza : function ( type ) {
37+ if ( type == 'cheese' ) {
38+ return fromPrototype ( Pizza , {
39+ description : 'Cheesy, Generic Pizza'
40+ } ) ;
41+ } else if ( type == 'veggie' ) {
42+ return fromPrototype ( Pizza , {
43+ description : 'Veggie, Generic Pizza'
44+ } ) ;
45+ }
46+ }
47+ } ;
48+
49+ var ChicagoPizzaStore = fromPrototype ( PizzaStore , {
50+ createPizza : function ( type ) {
51+ if ( type == 'cheese' ) {
52+ return fromPrototype ( Pizza , {
53+ description : 'Cheesy, Deep-dish Chicago Pizza'
54+ } ) ;
55+ } else if ( type == 'veggie' ) {
56+ return fromPrototype ( Pizza , {
57+ description : 'Veggie, Deep-dish Chicago Pizza'
58+ } ) ;
59+ }
60+ }
61+ } ) ;
62+
63+ var CaliforniaPizzaStore = fromPrototype ( PizzaStore , {
64+ createPizza : function ( type ) {
65+ if ( type == 'cheese' ) {
66+ return fromPrototype ( Pizza , {
67+ description : 'Cheesy, Tasty California Pizza'
68+ } ) ;
69+ } else if ( type == 'veggie' ) {
70+ return fromPrototype ( Pizza , {
71+ description : 'Veggie, Tasty California Pizza'
72+ } ) ;
73+ }
74+ }
75+ } ) ;
76+
77+ // Elsewhere in our app...
78+ var chicagoStore = Object . create ( ChicagoPizzaStore ) ;
79+ var pizza = chicagoStore . createPizza ( 'veggie' ) ;
80+ console . log ( pizza . description ) ; // returns 'Veggie, Deep-dish Chicago Pizza'
0 commit comments