1

Call the parent method.How to implement?

 function Ch() {
        this.year = function (n) {
            return n
        }
    }

    function Pant() {
        this.name = 'Kelli';
        this.year = function (n) {
            return 5 + n
        }
    }

//extends

 Pant.prototype = new Ch();
    Pant.prototype.constructor = Pant;
    pant = new Pant();
    alert(pant.name); //Kelli
    alert(pant.year(5)) //10

How to сall the parent method

this.year = function (n) {
            return 5 + n
        } 

in object?Thank you all for your help

zloctb
  • 10,592
  • 8
  • 70
  • 89
  • 1
    `Pant`? `Parent`? Are these the same? If so, does `Ch` stand for `Child`? If it does, then your naming is very confusing since the Parent in inheriting from the Child. – I Hate Lazy Nov 08 '12 at 20:44
  • 1
    ...anyway, you're calling `.year()` successfully, so I'm not sure what your question actually is. – I Hate Lazy Nov 08 '12 at 20:49
  • Have a look at [this answer](http://stackoverflow.com/a/13167717/1048572) – Bergi Nov 08 '12 at 20:54
  • Why is your `year` method not on the prototype of `Ch`? If there is a reason, please show us your actual code; else move it. – Bergi Nov 08 '12 at 21:00
  • All methods must be in prototype?Why? – zloctb Nov 08 '12 at 21:06
  • @zloctb: They need not, but should. Else every instantiation would create a new function - see [prototype vs this](http://stackoverflow.com/q/310870/1048572) – Bergi Nov 08 '12 at 22:41

4 Answers4

1

You can call overridden supper class(Parent) methods using __proto__ but it is not supported by IE

alert(pant.__proto__.year(5)) //5
Anoop
  • 23,044
  • 10
  • 62
  • 76
1

Here is how Google's Closure Library implements inheritance:

goog.inherits = function(childCtor, parentCtor) {
  function tempCtor() {};
  tempCtor.prototype = parentCtor.prototype;
  childCtor.superClass_ = parentCtor.prototype;
  childCtor.prototype = new tempCtor();
  childCtor.prototype.constructor = childCtor;
};

Your code would then become something like:

function Ch() {}
Ch.prototype.year = 
function (n) {
   return n
}

function Pant() {}
goog.inherits(Pant,Ch);
Pant.prototype.name = 'Kelli';
Pant.prototype.year = function (n) {
   return 5 + Pant.superClass_.year.call(this, n);//Call the parent class
}

pant = new Pant();
alert(pant.name); //Kelli
alert(pant.year(5)) //10

You could of course rename the goog.inherits function if you wanted.

Joshua Dwire
  • 5,415
  • 5
  • 29
  • 50
1

Adapting this answer to your code:

function Ch() {
    this.year = function(n) {
        return n;
    }
}

function Pant() {
    Ch.call(this); // make this Pant also a Ch instance
    this.name = 'Kelli';
    var oldyear = this.year;
    this.year = function (n) {
        return 5 + oldyear(n);
    };
}
// Let Pant inherit from Ch
Pant.prototype = Object.create(Ch.prototype, {constructor:{value:Pant}});

var pant = new Pant();
alert(pant.name); // Kelli
alert(pant.year(5)) // 10
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • The questions is confusing, but I believe OP just wants to call `Pant.prototype.year` (considering the `prototype` from the original code, not the one returned by `Object.create`). – bfavaretto Nov 08 '12 at 21:01
  • @bfavaretto: Yes, it is confusing. Yet the method is not on the prototype, unfortunately... – Bergi Nov 08 '12 at 21:04
1

First of all, assuming Ch is for "child", and Pant for "parent", you are doing it backwards, which is extremely confusing. When you say

Pant.prototype = new Ch();

You're making Pant inherit from Ch. I'm assuming that's really what you mean, and that you want to call the method that returns n, instead of the one that returns n + 5. So you can do this:

function Ch() {
    this.year = function (n) {
        return n;
    }
}

function Pant() {
    this.name = 'Kelli';
    this.year = function (n) {
        return 5 + n;
    }
}

Pant.prototype = new Ch();
Pant.prototype.constructor = Pant;
pant = new Pant();
alert(pant.name); //Kelli
alert(pant.year(5)) //10

// Is the below what you need?
alert(Pant.prototype.year(5)); // 5

http://jsfiddle.net/JNn5K/

bfavaretto
  • 71,580
  • 16
  • 111
  • 150