je lisais ce article, et je pris l'exemple et fait une très petite modificationjavascript héritage par prototype
function Parent(p1, p2) {
console.log('run here');
var prop1 = p1; // private property
this.prop2 = p2; // public property
// private method
function meth1() { return prop1; }
// public methods
this.meth2 = function(){ return this.prop2; };
this.meth6 = function(){ return meth1(); };
}
Parent.prototype.hello=function(){console.log('hi '+this.prop2);};
function Child(p1, p2, p3, p4) {
Parent.apply(this, arguments); // initialize Parent's members
this.prop3 = p3;
this.meth3 = function(){ return this.prop3; }; // override
var prop4 = p4;
this.meth4 = function(){ return prop4; };
}
Child.prototype = new Parent(); // set up inheritance relation
// console.log(Child.prototype)
var cc = new Child("one", "two", "three", "four");
console.log(cc)
var result1 = cc.meth6(); // parent property via child
var result2 = cc.meth2(); // parent method via child
var result3 = cc.meth3(); // child method overrides parent method
var result4 = cc.hello();
console.log(result1,result2,result3,result4);
Le problème est même méthode cc.hello semble exister, mais quand je l'appelle, la console retour non défini, quelqu'un peut-il expliquer pourquoi? grâce
La méthode 'hello' n » t retourne rien, donc 'non défini '. Je ne sais pas ce que vous attendez ... – elclanrs