Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/286063-sub-class-inheritance/
Share on other sites

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());

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.