简体   繁体   English

如何在子原型中重写父原型 function?

[英]How to rewrite parent prototype function in a child prototype?

I'm learning prototypes in JS and I have troubles trying to rewrite my parent prototype function in my child prototype.我正在学习 JS 中的原型,我在尝试在我的子原型中重写我的父原型 function 时遇到了麻烦。

In the code below, I'm trying to rewrite the function presentation from my class Personn in order to display the new Etudiant property 'etablissement' as well.在下面的代码中,我试图从我的 class Personn 重写 function 演示文稿,以便也显示新的 Etudiant 属性“etablissement”。

 function Personne(nom, age, sexe){ this.nom = nom; this.age = age; this.sexe = sexe; } Personne.prototype.presentation = function() { return 'Bonjour, je suis ', this.nom + ', ' + this.sexe + ' de ' + this.age + ' ans.'; } function Etudiant(nom, age, sexe, etablissement){ Personne.call(this, [nom, age, sexe]); this.etablissement = etablissement; } Etudiant.prototype = Object.create(Personne.prototype); Etudiant.prototype.constructor = Etudiant; Etudiant.prototype.presentation = function (){ return Personne.prototype.presentation.call(this) + ' Je travaille au ' + this.etablissement + '.'; }; let patrick = new Etudiant('patrick', 26, 'etoile de mer', 'Club'); console.log(patrick.presentation()); // this displays 'patrick,5651,etoile de mer, undefined de undefined ans. Je travaille au Club.'

The problem is here:问题在这里:

Personne.call(this, [nom, age, sexe]);

With call , you pass discrete arguments, not an array of arguments.使用call ,您传递离散的 arguments,而不是 arguments 的数组。 Either change that to use apply , which does take an array (or any array-like):将其更改为使用apply ,它确实需要一个数组(或任何类似数组):

Personne.apply(this, [nom, age, sexe]);

or make the arguments discrete:或使 arguments 离散:

Personne.call(this, nom, age, sexe);

Live Example:现场示例:

 function Personne(nom, age, sexe){ this.nom = nom; this.age = age; this.sexe = sexe; } Personne.prototype.presentation = function() { return 'Bonjour, je suis ', this.nom + ', ' + this.sexe + ' de ' + this.age + ' ans.'; } function Etudiant(nom, age, sexe, etablissement){ Personne.call(this, nom, age, sexe); this.etablissement = etablissement; } Etudiant.prototype = Object.create(Personne.prototype); Etudiant.prototype.constructor = Etudiant; Etudiant.prototype.presentation = function (){ return Personne.prototype.presentation.call(this) + ' Je travaille au ' + this.etablissement + '.'; }; let patrick = new Etudiant('patrick', 26, 'etoile de mer', 'Club'); console.log(patrick.presentation()); // this displays 'patrick,5651,etoile de mer, undefined de undefined ans. Je travaille au Club.'


Side note: If you're going to use constructor functions and the prototype property, in modern JavaScript (ES2015+) you can do that more easily with class syntax:旁注:如果您要使用构造函数和prototype属性,在现代 JavaScript (ES2015+) 中,您可以使用class语法更轻松地做到这一点:

 class Personne { constructor(nom, age, sexe){ this.nom = nom; this.age = age; this.sexe = sexe; } presentation() { return 'Bonjour, je suis ', this.nom + ', ' + this.sexe + ' de ' + this.age + ' ans.'; } } class Etudiant extends Personne { constructor(nom, age, sexe, etablissement) { super(nom, age, sexe); this.etablissement = etablissement; } presentation() { return super.presentation() + ' Je travaille au ' + this.etablissement + '.'; } } let patrick = new Etudiant('patrick', 26, 'etoile de mer', 'Club'); console.log(patrick.presentation()); // this displays 'patrick,5651,etoile de mer, undefined de undefined ans. Je travaille au Club.'

It creates effectively the same thing (there are some minor differences, the main one being the constructors can't be called as normal functions — which you usually don't want them to be).它有效地创建了相同的东西(有一些细微的区别,主要的区别是构造函数不能被称为普通函数——你通常不希望它们如此)。 Importantly, it still uses prototypical inheritance, constructor functions, and the prototype property;重要的是,它仍然使用原型 inheritance、构造函数和prototype属性; the syntax just makes it easier to set things up, and more declarative.语法只是使设置变得更容易,并且更具声明性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM