0

This is surely trivial question, but I am little confused with JavaScript. I want to call method inside another function. I have spent some time to handle this and all I have got so far is the second piece of code. I would like to know, if there is any similar solution as noted in comment.

function Student() {
  this.greeting = function() {
    alert('Hi! I\'m student.');
  };
};

function Teacher() {
  Student.call(this)
  this.greeting = function() {
    Student.???  // here I want something like "inherited", or call  Student.greeting()
    alert('and I like apples');
  };
};

I want to be sure, there is no another option like prototyping:

function Student() {

};

Student.prototype.greeting = function() {
    alert('Hi! I\'m student.');
  };

function Teacher() {
  Student.call(this);
  this.greeting = function() {
    Student.prototype.greeting();
        alert('and I like apples');
  };
};

Thanx

lyborko
  • 2,571
  • 3
  • 26
  • 54
  • This looks like you're trying to use shadowing with class inheritance which is not in the spirit of JavaScript which uses the prototype chain for linking objects. By delegating to other objects (using Object.create for example) you can access these methods through `[[Prototype]]' chaining. – Matt D. Webb Mar 26 '17 at 16:21

1 Answers1

-1

Student is a function, in your case it is also a constructor. To call "Student" you need to save object returned by call of constructor-function Student

function Teacher() {
  var stdnt = new Student()
  this.greeting = function() {
    stdnt.greeting()
    alert('and I like apples');
  };
};

Also, in your code Student.call(this) is equivalent to just function call Student() - without creation of new object

Upd2. This call of stdnt from inner function called "closure"

nahab
  • 1,308
  • 17
  • 38
  • So the the only possibility is to instantiate Student function? – lyborko Mar 26 '17 at 16:10
  • In your example, greeting is a member function of student – Paul Mar 26 '17 at 16:12
  • Except your variant? Yes, you have to instantiate new object by calling Student constructor. Of cause there can be other ways, for example like this – nahab Mar 26 '17 at 16:15
  • the "call" function will take properties (there are none now) from Student and then I can use them in the Teacher. But I can not do the same with function (method)... and that is frustrating... – lyborko Mar 26 '17 at 16:16
  • var Student = {greeting: function (){alert("Student"}} – nahab Mar 26 '17 at 16:17
  • No call will not take props from Student. It will be vice versa – nahab Mar 26 '17 at 16:18
  • What call function is actually do is setup current context inside Student function for this particular case. In your case this context inside Student function will be equal to context of Teacher function, as you pass context of Teacher ("this") to it – nahab Mar 26 '17 at 16:21
  • Thanx. Javascript is sometimes pretty tricky... :-) – lyborko Mar 26 '17 at 16:31
  • @Paul greeting is not a member of function Student. It a member of context passed to Student – nahab Mar 26 '17 at 16:42