UBER

Access to the parent from a child object in OO JS

"This could be convenient when a child wants to have 
a method that does everything the parent's method does plus something in addition. 
By doing so, the child calls the parent's method with the same name and works with the result."

Objiect-Oriented JavaScript by Stoyan Stefanov 

UBER 

- Access to the parent from a child object

                     var parent = {
                         name: 'Matt',
                         showInfo: function(){
                              return this.name;
                         }

                     }

                                                
                     var child = {};     /*  Create a variable that will store our child object. */

UBER 

- Access to the parent from a child object

                     var F = function(){};  /* Create an empty function F which will act as an intermediary                                                                                                                           between the parent and the child. */

  

                     F.prototype = parent; /*  This will allow the child to copy all the parent'€™s properties                                                                                                                                 without having to set it€™s prototype to the parent instance.. */

 

                     var child = new F();  /*  We get a copy of all the parent'€™s properties. Functions/objects are                                                                                                                  copied reference. */                                                                                       

 

UBER 

- Access to the parent from a child object

Why a temporary constructor...

When a prototype is copied ( Ex. obcject1.prototype = object2.prototype ) we gain a performance boost (for reasons that are out of scope of this presentation); however if we change a child prototype value, the prototype chain for that value property will inherit the changes.

This surely would suit some cases but not all, specifically not ours.

UBER 

- Access to the parent from a child object

                     child.uber = parent;  /* property uber which points to the parent object */

  

                     child.name = 'Miklos'

 

                     child.showInfo = function(){
                               return this.uber.showInfo() + ' child is: ' +                                                  this.name;

                     }        

 

                     console.log( child.showInfo() ); ==> "Matt child is: Miklos"                                                            

                    http://www.alessandrosantese.com/uber.html

                     Any UBER related questions??

Extra resources

 

1) BOOK: Object-oriented JavaScript http://www.amazon.com/Object-Oriented-JavaScript-Edition-Stoyan-Stefanov/dp/1849693129

 

2) LINKS:

 - http://www.crockford.com/javascript/inheritance.html

 - http://adventuresincoding.com/2010/07/prototypal-inheritance-in-    javascript-explained

Uber

By Alessandro Santese