I'm trying to get a deeper hold on prototypal inheritance and class creation (I know, there are other ways, but for the purpose of this I'm trying to grasp prototypes.) My question is: Using the following code example, is there a way to create private variables inside of Tree and Fruit that will not be returned with the function, but is still accessible to the prototype functions genus and bulk?
var Tree = function ( name, size ) { this.name = name; this.size = size; }; Tree.prototype.genus = function(){ return ((typeof this.name !== 'undefined') ? this.name : 'Hybridicus Maximus'); }; Tree.prototype.bulk = function(){ return ((typeof this.size !== 'undefined') ? this.size : '8') + ' ft'; }; var Fruit = function( name, size ) { this.name = name; this.size = size; }; Fruit.prototype = new Tree(); // Fruit.prototype = Tree.prototype; -- I know this can be used, too. Fruit.prototype.bulk = function(){ return ((typeof this.size !== 'undefined') ? Math.floor(this.size / 2) : '4') + ' lbs'; }; var pine = new Tree('Pine', 9); var apple = new Fruit('Apple', 6); console.log(pine.genus(), pine.bulk()); // Outputs: "Pine 9 ft" console.log(apple.genus(), apple.bulk()); // Outputs: "Apple 3 lbs" EDIT: I'm trying to replace this.name and this.size with private variables that can be accessed in the prototype functions. Sorry for the lack of clarity!