不同文件中构造函数之间的部分继承(JavaScript)? [英] Partial inheritance between constructors in different files (JavaScript)?

查看:37
本文介绍了不同文件中构造函数之间的部分继承(JavaScript)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个不同的js文件中有两个构造函数.我希望构造函数具有两个相同的方法(名称和功能相同),并且一个方法被称为相同的方法但工作方式不同(名称相同但功能相同).这两个构造函数中还将有两个其他相同的属性.

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.

代码类似于以下内容:

文件1 :(有人建议删除第一行以避免循环,但这似乎没有任何作用)

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;

文件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;

所以,我想这更像是两个问题.1)这是解决我的问题的好方法吗?2)当构造函数位于单独的文件中时,为什么它不起作用?在Webstorm中,我注意到方法调用(在Parent.call中)是未解决的函数或方法调用.

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.

提前谢谢!

推荐答案

此处的示例存在两个问题.

There's a couple problems with your example here.

  1. 两个文件之间具有循环依赖关系,因为它们都相互依赖.总的来说,这是一个坏主意.文件不应该相互依赖,因为这是导致意外行为的秘诀.
  2. 您需要使用第3个构造函数来解决称为 Person 的问题. Parent Child 都应从 Person 继承,您应该在 Person 中定义通用方法,它们在各个文件上的不同之处.
  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());

记录了以下内容:

Armageddon
Deep Impact

如果您不明白这一点,我认为问题是您如何使用课程.

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

这篇关于不同文件中构造函数之间的部分继承(JavaScript)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆