[RESOLVED]Best method to polymorphism of javascript objects?

javascript equivalent c# code

class Animal 
{
    string Name;
    Animal(string name)
    {
        this.Name = name;
    }
    virtual void Run()
    {
        Response.Write(this.Name + " runs.");
    }
    virtual void Say()
    {
        Response.Write(this.Name + " says.");
    }
}
class Dog : Animal
{
   override void Say()
   {
       base.Say();
       Response.Write(" as bark");
   }
}

...
var mydog = new Dog("chihuahua");
mydog.Run; mydog.Say();

I write javascript code as:

function Animal(name) {
                this.Name = name;
                this.Say = function () { console.log(this.Name + ' says'); }
            }
            function Dog(name) {
                Animal.call(this, name);
                this.Say= function () {
                    Animal.Say.call(this);
                };
            }
            var mydog = new Dog('chihuahua');
            mydog.Say();

but it said Animal.Say is undefined.

If I write code as

function Animal(name) {
                this.Name = name;
            }
            Animal.prototype.run = function () { console.log(this.Name + ' runs'); }
            function Dog(name) {
                Animal.call(this, name);
            }
            var mydog = new Dog('chihuahua');
            mydog.run();

I got mydog.run is undefined.

So if I use Base.prototype.action = function() approach, I have to write sub.prototype.action for each methods of each sub classes, even they are same as base type.

If I use this.action = function() in base type prototype, I can use sub.action directly, no need to write them, however, when I need to do some polymorphism to override base method, I can’t find a way to call base method in sub method.

Hi,

Refer following code for polymorphism in javascript

 function Student(Name, Standard) {
          this.Name = Name;
          this.Standard = Standard;
          this.getDetails = function () {
              return "I am student my name is " + this.Name + " I am student of Class " + this.Standard;
          }
      }
      function Teacher(Name, Standard, Salary) {
          this.salary = Salary;
          this.Name = Name;
          this.Standard = Standard;
          this.getInfo = function () {
              return "I am Teacher my name is " + this.Name +
                "I teach to Class " + this.Standard + "and I earns " + this.salary;
          }
      }

      Teacher.prototype = new Student();
      Teacher.prototype.constructor = Teacher;

      function getDetails(obj) {
          alert(obj.getDetails());
      }

      var Student = new Student('Student1', 'Class1');
      var Teacher = new Teacher('Teacher1', 'Class2', 50000);
   
      getDetails(Student);
      getDetails(Teacher);

You can try it at URL

http://jsfiddle.net/atyhxtj2/

Example more relevant to your Question is as follows,

function Animal(Name) {
          this.Name = Name;
          this.Run = function () {
              return "My name is " + this.Name + " I am Running";
          }
          this.Say = function () {
              return "My name is " + this.Name + " I am may be bark or say also";
          }
      }
      function Dog(Name) {
          this.Name = Name;
          this.Run = function () {
              return "My name is " + this.Name + " I am Running";
          }
          this.Say = function () {
              return "My name is " + this.Name + " I Bark";
          }
      }
      Animal.prototype = new Dog();
      Animal.prototype.constructor = Animal;

      function Run(obj) {
          alert(obj.Run());
      }
      function Say(obj) {
          alert(obj.Say());
      }

      var objAnimal = new Animal('Genaral Animal');
      var objDog = new Dog('Dog');
      Say(objAnimal);
      Say(objDog);

You can try it at url

http://jsfiddle.net/1fvhe3ou/

Regards,

Ajay

hi Cheng,

try the following code:

 <script type="text/javascript">
        Animal=function (name) {
            this.Name = name;
         
        }
        Animal.prototype.Say=function () { alert(this.Name + ' says'); }
        Dog=function (name) {
            Animal.call(this, name);
          
           
        }

        function init()
        {
          
            var mydog = new Dog('chihuahua');
            Dog.prototype = Object.create(Animal.prototype);
            mydog.Say = function () {
                Animal.prototype.Say.call(this);
            };
            mydog.Say();
            

        }
</script>

just one statement was required using the Object.create method to inherit the Animal class.

Works perfectly.

Working sample

hope it helps 

Ajay230784

Hi, the problem of your answer is if I don’t need to override Say() in function Dog, base method Say() is enough for me, So I remove the line

this.Say = function () {
              return "My name is " + this.Name + " I Bark";
          }

from function Dog.

Then I get TypeError: obj.Say is not a function error.

So for a complex object, I have to override all methods for all sub classes

shivigupta31web

hi Cheng,
try the following code:

same, if I remove the line, so I don’t need to override Say,

mydog.Say = function () {
                Animal.prototype.Say.call(this);
            };

I got TypeError: mydog.Say is not a function error

I make changes on my first example

function Animal(name) {
    this.Name = name;
    this.Say = function () { console.log(this.Name + ' says'); }
}
function Dog(name) {
    Animal.call(this, name);
    this.Say= function () {
        base_obj.Say.call(this);
        console.log(' and bark too');
    };
    var base_obj = new Animal();
}            

var mydog = new Dog('chihuahua');
mydog.Say();

works

and if I don’t want override Say

function Animal(name) {
    this.Name = name;
    this.Say = function () { console.log(this.Name + ' says'); }
}
function Dog(name) {
    Animal.call(this, name);
    //this.Say= function () {
    //    base_obj.Say.call(this);
    //    console.log(' and bark too');
    //};
    var base_obj = new Animal();
}            

var mydog = new Dog('chihuahua');
mydog.Say();

works too.

seems a little lame to me, have to keep a redundant base_obj for reference. I wonder if javascript has language ability like c# to let subclass choose whether to override (and call base) or not at will.

Hi Cheng,

Try the following rectified code, you dont have to override the Say method just to complete the inheritance and making methods of base class accessible to the derived class we have to do this:

 Animal=function (name) {
            this.Name = name;
                  }
        Animal.prototype.Say=function () { alert(this.Name + ' says'); }
      
  Dog=function (name) {
            Animal.call(this, name);
                      
        }
          
          
            Dog.prototype = Object.create(Animal.prototype);
            Dog.prototype.constructor = Dog;
            var mydog = new Dog('chihuahua');
            mydog.Say();
            

hope this helps somehow.

For more details check out the following link:

 

Leave a Reply