-1

I have two javascript classes as

class1 = function(opt) {
      function abc () {
      }
      function def () {
      }
      function xyz () {
      }
};


class2 = function(opt) {
          function abc () {
          }
          function def () {
          }
          function lmn () {
          }
    };

These two classes contain some common methods like (abc, def) and some specific methods like (lmn, xyz). Can anybody suggest me how to apply inheritance to this situation effectively, so that I can have common methods in one file and specific methods in respective files. I tried prototype method, but this is not working. So is there any other way to this. Thanks.

HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
sonam
  • 875
  • 2
  • 18
  • 37
  • 2
    Please show us how those functions are "methods" of the "classes". Currently they're just private functions inside other functions, and moving them into a common scope would solve the problem. Please also show the prototype method you've tried, then we can help you more specifically about your mistakes. – Bergi Jul 26 '13 at 12:53

2 Answers2

0

Javascript dont have classes

But you can systemise your code.Javascript inheritance is totally different from that of othe oop languages.

Here,We use prototypes and constructors.

**prototype==>**In simple words,I am used for extension purpose

**constructors==>**I am used for creating multiple instances.Any function can be used as a constructor by using the new keyword.

Just sample codes for understanding.

SAMPLE 1:BY USING OBJECT LITERAL

var Myobject = {

    Function_one: function()
    {
        //some code
        Myobject.function_three();
    },
    Function_two: function()
    {
        //some code
        Myobject.function_three();//lets say i want to execute a functin in my object ,i do it this way...
    },
    Function_three: function()
    {
        //some code
    }
};

window.onload = Myobject.Function_one //this is how you call a function which is in an object

SAMPLE 2:BY USING PROTOTYPE

function function_declareVariable()
{
   this.a= 10; //i declare all my variable inside this function
   this.b= 20;
}

function_declareVariable.prototype.Function_one = function()
{
  //some code
    Myobject.Function_three();
};

function_declareVariable.prototype.Function_two = function()
{
    Myobject.Function_three();
};

function_declareVariable.prototype.Function_three = function()
{
   alert(Myobject.a or Myobject.b)
   //some code
};

var Myobject = new function_declareVariable();//this is how i instantiate

REFER 1:what are constructors ,prototypes

REFER 2:prototypal inheritance

Community
  • 1
  • 1
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
0

Depending on whether these classes just share behavior (interface) or are actually subclasses of a common class, you should use either a mixin or prototypal inheritance respectively.

An example for prototypes:

function BaseClass () {
}

BaseClass.prototype = {
    abc: function () {
    },

    def: function () {
    }
};

function class1 () {
}

class1.prototype = new BaseClass();

class1.prototype.xyz = function () {
};

function class2 () {
}

class2.prototype = new BaseClass();

class2.prototype.lmn = function () {
};

And an example of mixins:

function BaseMixin (object) {
    object.abc = BaseMixin.prototype.abc;
    object.def = BaseMixin.prototype.def;
}

BaseMixin.prototype = {
    abc: function () {
    },

    def: function () {
    }
};


function class1 () {
    BaseMixin(this);
}

class1.prototype = {
    xyz: function () {
    }
};

function class2 () {
    BaseMixin(this);
}

class2.prototype = {
    lmn: function () {
    }
};
quinnirill
  • 814
  • 4
  • 6