Partial inheritance between constructors in different files (JavaScript)?

。_饼干妹妹 提交于 2021-01-29 04:55:25

问题


I have two constructors in two different js files. I want the constructors to have two methods that are the same (same name and functionality) and one method that is called the same but works differently (same name but not the same functionality). There will also be a couple of other properties that are the same in both constructors.

The way I have tried to achieve this is by having one of the constructors inherit the other and then declare a new method with the same name as an old one (see below). This works well enough (I believe) when the constructors are in the same file, but when they are in separate files I seem to lose the connection. The inheritance doesn't work.

The code looks similar to this:

File 1: (someone suggested removing the first line to avoid circulation, but it didn't seem to make any difference)

var Child = require("./Child");

var Parent = function(name, age) {
   this.name = name;
   this.age = age;
};

Parent.prototype.calculate1 = function(a, b) {
   return a + b;
};

Parent.prototype.calculate1 = function(a, b) {
   return a - b;
};

Parent.prototype.calculate3 = function() {
   return "Armageddon";
};

module.exports = Parent;

File 2:

var Parent = require("./Parent");

var Child = function(name, age) {
   Parent.call(this, name, age);
};

Child.prototype = Object.create(Parent.prototype);

Child.prototype.calculate3 = function() {
   return "Deep Impact";
};

module.exports = Child;

So, I guess it's more like two questions. 1) Is this a good way to solve my problem? And 2) why doesn't it work when the constructors are in separate files? In Webstorm I get a notice that the method call (in Parent.call) is an unresolved function or method call.

Thanks in advance!


回答1:


There's a couple problems with your example here.

  1. You have circular dependencies between your two files, as in they both rely on each other. In general, that's a bad idea. Files should not BOTH depend on each other as that is a recipe for unexpected behavior.
  2. You need to use a 3rd constructor to solve your problem called Person or something. Both Parent and Child should inherit from Person, and you should define the common methods in Person, and the ones where they differ on the individual files.

Here's an example of how it should probably work:

// Person.js
function Person (name, age) {
  this.name = name;
  this.age = age;
}

Person.prototype.someShareMethod = function (a, b) {
  return a + b;
};

module.exports = Person;

// Parent.js
var Person = require('./Person');

function Parent (name, age) {
  Person.call(this, name, age);
}

Parent.prototype = Object.create(Person.protoype);

Parent.prototype.someSelfMethod = function (a, b) {
  return a * b;
};

module.exports = Parent;

// Child.js
var Person = require('./Person');

function Child (name, age) {
  Person.call(this, name, age);
}

Child.prototype = Object.create(Person.protoype);

Child.prototype.someSelfMethod = function (a, b) {
  return a - b;
};

module.exports = Child;

All that being said, the general concept of your inheritance should work. Once constructor can inherit from another, and that is a perfectly good practice. I broke out your code (with circular reference removed), ran this locally, and got the expected output:

var Parent = require('./Parent'),
    Child = require('./Child');

var p = new Parent('Bob', 40);

console.log(p.calculate3());

var c = new Child('Stan', 12);

console.log(c.calculate3());

Which logged this:

Armageddon
Deep Impact

If you didn't get that, I think the problem is how you've used your classes.



来源:https://stackoverflow.com/questions/33601909/partial-inheritance-between-constructors-in-different-files-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!