Iluvatar+ Posted February 9, 2014 Share Posted February 9, 2014 Hi guys i am trying to help out a friend with a bit of work and there is a question i am unsure of... "Define an object Animal that has a member function walk(). Then define an object Elephant that is a sub-class of Animal. Define a member function trumpet() on the Elephant object. Create an instance of the Elephant object and call both functions on that instance." Here is what my thoughts were... function Animal() { this.walk = function() { return 'walk walk walk'; } } function Elephant() { this.prototype = new Animal(); this.trumpet = function(){ return 'splash splash'; }; return this; } var Elephant1 = new Elephant(); console.log(Elephant1.prototype.walk()); console.log(Elephant1.trumpet()); I am unsure if i am going to right way about this. Quote Link to comment https://forums.phpfreaks.com/topic/286063-sub-class-inheritance/ Share on other sites More sharing options...
kicken Posted February 9, 2014 Share Posted February 9, 2014 Typically you define the prototype outside of the constructor. eg: function Animal(){} Animal.prototype = { walk: function(){ return 'walk walk walk'; } }; function Elephant(){} Elephant.prototype = new Animal(); Elephant.prototype.trumpet = function(){ return 'splash splash'; }; The constructor need not return anything, returning 'this' is the default action. When you call the function, you do not involve the prototype, just call the function directly on the object var Elephant1=new Elephant(); console.log(Elephant1.walk()); console.log(Elephant1.trumpet()); Quote Link to comment https://forums.phpfreaks.com/topic/286063-sub-class-inheritance/#findComment-1468287 Share on other sites More sharing options...
Iluvatar+ Posted February 9, 2014 Author Share Posted February 9, 2014 Why would we prototype the walk() method though? Quote Link to comment https://forums.phpfreaks.com/topic/286063-sub-class-inheritance/#findComment-1468289 Share on other sites More sharing options...
kicken Posted February 9, 2014 Share Posted February 9, 2014 When you add methods to a type in Javascript you generally want to put them on the prototype for efficiency reasons. The prototype is shared among all objects created of that type, so only one copy of the method is needed in memory. If you add methods via the constructor using this.blah = function(){...} then you are creating a new copy of the function for each instance of the type you create, wasting memory space. The benefit of using the this.blah = function(){...} method is you can emulate private instance variables by creating closures and using a variable within the constructor. Eg: function Counter(){ var count=0; this.increment=function(){ return ++count; } this.decrement=function(){ return --count; } } var c = new Counter(); c.increment(); Doing things that way prevents you from modifying the count variable except via those methods at the expense of a bit more memory creating copies of the methods. When using the more traditional prototype method of function Counter(){ this.count=0; } Counter.prototype = { increment: function(){ return ++this.count; } decrement: function(){ return --this.count; } }; var c=new Counter(); c.increment(); Then the increment/decrement methods are shared among all instances of Counter but the state variable count is accessible by doing c.count. In order to keep the shared memory aspect of the prototype method, but still indicate an instance variable is private (or protected) some people prefix the variable with a _. The _ doesn't mean anything to the JS engine but it's a symbol to the developer that the property should not be accessed directly and if it is, bad things could happen. Quote Link to comment https://forums.phpfreaks.com/topic/286063-sub-class-inheritance/#findComment-1468311 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.