0

I know this question was answered so many times, but I am so confused about how to inherit (yes, inherit....again) two Javascript functions. Assumed that I have a class called 'Base' which is the one that I want to inherit;

function Base(model)
{
  var self=this;

  self._model=model;

  return self;
}

Base.prototype.modelName= function()
              {
                   return  self._model.Name; 
              };

Then I create a new class call Foo.

function Foo()
{
  var self=this;

   self.hello=function()
   {
          return 'Hello World';
   }

   return self;
}

What code should I add to the Foo class to be able to do something like this? var myModel={type:1, name:'My Model'};

var myObj=new Foo(myModel);
var  result= MyObj.modelName();

I know I should use the object.create() method, but I cannot understand exactly how! :(

Thank you guys, and again sorry for this silly question.....I am really struggling with this basic concept here!!!!

  • 1
    Have you actually Googled "Javascript Inheritance examples" and not found a single example? There are thousands of examples there. Some basic internet research should be attempted before you post here. And, then, if you are still stuck after that basic research, you should tell us exactly what you tried and where you got stuck. – jfriend00 Apr 13 '15 at 05:13
  • Also relevant duplicate question/answer [JavaScript inheritance with Object.create()?](http://stackoverflow.com/questions/3079887/javascript-inheritance-with-object-create) and an answer worth reading and understanding [Javascript inheritance: call super-constructor or use prototype chain?](http://stackoverflow.com/questions/4152931/javascript-inheritance-call-super-constructor-or-use-prototype-chain/4389429#4389429). – jfriend00 Apr 13 '15 at 05:28
  • jfriend00, thank you....but instad of having this attitude, you could spend 5 seconds of your time to explain with 2 simple line of codes how this thing works (as vigneswaran-marimuthu did). You are not obligated to answer to any questions. I completely understand that you marked my question as duplicated (I knew it from the beginning), but give me a 'lecture' instead of answer to my question is actually not helpful. Again, if you don't want to waste your time on elementary question like mine was, just don't do it. Anyway, thank you for your precious comments. – Militello Vincenzo Apr 13 '15 at 05:35
  • No - that is not how this site works. They don't accept questions that do not show an attempt at basic research and effort. And, don't accept questions that have been asked many other times here. You will need to learn these guidelines if you want to successfully use this site. Your answer is very easily seen in a simple Google search which illustrates you did not do the most basic research on your problem. I did provide you links to three other answers that all explain how JS inheritance works in great detail. StackOverflow does not want to duplicate answers that have already been covered. – jfriend00 Apr 13 '15 at 05:37
  • Again....my problem was to not understand completely how the inheritance works...even after read a lot of post on google. That's it, I don't have to justify myself for what I've asked and why. Thank you for you time. – Militello Vincenzo Apr 13 '15 at 05:41
  • And, I am also spending effort to help educate you on how this site works so you can more successfully use it in the future by both asking questions and by hopefully answering questions too. This should help you get the most out of this site and help contribute to making StackOverflow a more valuable resource. – jfriend00 Apr 13 '15 at 05:42
  • 1
    I think that there are, perhaps, far better answers out there than anything I might put here, not least of which is [MDN's own article](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain) on the subject of JavaScript inheritance. – Thomas Preston Apr 13 '15 at 05:43
  • I would also add that, right off the bat, you should take a step back and remove "class" from your lexicon when speaking about JavaScript OOP, as JavaScript is a prototypal language. See the answer to this previous question: http://stackoverflow.com/questions/816071/prototype-based-vs-class-based-inheritance and this other MDN article, [Details of the Object Model](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model) – Thomas Preston Apr 13 '15 at 05:44
  • You did not even show what you tried with `Object.create()`. Not one single line of code with it. The first ten answers to a Google search for ["javascript inheritance object.create"](https://www.google.com/search?q=javascript+inheritance+object.creatre&rlz=1C1CHFX_enUS515US515&oq=javascript+inheritance+object.creatre&aqs=chrome..69i57j0l4.4855j0j7&sourceid=chrome&es_sm=93&ie=UTF-8#q=javascript+inheritance+object.create) all show you how to do this. – jfriend00 Apr 13 '15 at 05:44

1 Answers1

1

JavaScript uses prototypal inheritance. To inherit from Base class, use like

Foo.prototype = new Base();

But you can't pass parameters into Foo. You need to move model save logic to Foo.

NOTE: Don't return the created object from constructor functions. By default, created object is returned.

EDITED

function Base() {

}

Base.prototype.modelName = function () {
    return  self._model.Name; 
};

function Foo(model)
{

    this._model = model

    this.hello = function () {
        return 'Hello World';
    }

}

Foo.prototype = new Base();
Vigneswaran Marimuthu
  • 2,452
  • 13
  • 16
  • Thank you! The fact is that the 'model' variable is something that make sense to be on my base class. I guess I should do something like Base.prototype.init = function(model){ /*my stuff here*/} – Militello Vincenzo Apr 13 '15 at 05:30
  • Yeah. You can do that and you can call superclass init from subcalss with entire arguments list :) – Vigneswaran Marimuthu Apr 13 '15 at 05:36