2

我正在试验(对我而言)一种新型的继承。我想用一个决定返回哪个继承者对象的参数来调用继承的函数。

这是一个示例来说明我的意思:

function Localizor(type) {
  this.language = "English"
  this.endonym = "English"

  if (window[type]) {
    return new window[type]()
  }
}
Localizor.prototype.native = function native() {
  return "I speak " + this.endonym
}
Localizor.prototype.english = function () {
  return "I speak " + this.language
}


function French () {
  this.language = "French";  
  this.endonym = "français"
}
French.prototype = new Localizor()
French.prototype.native = function french() {
  return "Je parle " + this.endonym
}


function Thai () {
  this.language = "Thai";  
  this.endonym = "ไทย"
}
Thai.prototype = new Localizor()
Thai.prototype.native = function thai() {
  return "พูดภาษา" + this.endonym
}

如果我new Localizor()在没有参数(或无效参数)的情况下调用,我会得到一个简单的英语对象。如果我用“法语”或“泰语”的参数调用它,我会得到一个对象,其中继承者覆盖了一些继承的方法,因此它说法语或泰语。例如:

var thai = new Localizor("Thai")
var feedback = thai.language + " | " + thai.endonym + " | " + thai.english() + " | " + thai.native()  
console.log(feedback)

这给了我输出Thai | ไทย | I speak Thai | พูดภาษาไทย

我有三个问题:
1. 这种类型的继承是否已经记录在某个地方(它有名字)吗?
2. 这样工作有什么危险吗?
3. 这个例子检查 的存在window[type],这在浏览器中工作时很好。如果这是在 node.js 的模块中,是否有确定模块中是否存在函数的等效方法?

编辑以回应 Zero21xxx

这是我发现的一种检测模块中是否存在构造函数的方法,但对我来说它看起来很危险的乱伦。它有什么风险?有什么更好的方法可用?

function extend(Child, Parent) {
  function F() {}
  F.prototype = Parent.prototype
  Child.prototype = new F()
  //Child.prototype.constructor = Child
  Child.parent = Parent.prototype
}

function Localizor(type) {
  this.language = "English"
  this.endonym = "English"

  this.French = function français () {
    this.language = "French";  
    this.endonym = "français"
  }
  extend(this.French, this)
  this.French.prototype.native = function french() {
    return "Je parle " + this.endonym
  }

  this.Thai = function ไทย () {
    this.language = "Thai";  
    this.endonym = "ไทย"
  }
  extend(this.Thai, this)
  this.Thai.prototype.native = function thai() {
    return "พูดภาษา" + this.endonym
  }

  if (typeof this[type] === "function") {
   return new this[type]()
  }
}
Localizor.prototype.native = function native() {
  return "I speak " + this.endonym
}
Localizor.prototype.english = function () {
  return "I speak " + this.language
}

module.exports = Localizor
4

2 回答 2

3

在我的拙见中,您应该有三个独立的构造函数,English它们继承自一个通用构造函数(我们称之为)。它看起来如下:FrenchThaiLocale

function Locale(constructor, language, endonym, native) {
    this.constructor = constructor;
    this.language = language;
    this.endonym = endonym;

    this.native = function () {
        return native + this.endonym;
    };
}

Locale.prototype.english = function () {
    return "I speak " + this.language;
};

function English() {}
function French() {}
function Thai() {}

English.prototype = new Locale(English, "English", "English", "I speak ");
French.prototype = new Locale(French, "French", "français", "Je parle ");
Thai.prototype = new Locale(Thai, "Thai", "ไทย", "พูดภาษา");

这导致关注点分离:每个构造函数只精确地执行它的预期用途。不多也不少。现在您可以创建一个localizer函数,如下所示:

function localizer(language) {
    switch (language) {
    case "French": return new French;
    case "Thai": return new Thai;
    default: return new English;
    }
}

因此,您需要做的就是调用localizer以获得所需的Locale.

于 2013-09-01T15:22:33.097 回答
0
  1. 是的,这是人们经常尝试以不同程度成功实现的“伪经典”继承。看看这个
  2. 那么你现在拥有它的方式有点难以阅读。您编写的代码说创建一个Localizor对象,但您会返回一个Thai对象。这可能很难追踪错误。
  3. 是的,有办法检查这些事情,这真的取决于你如何构建模块。

我的建议是像这样稍微重写一下

function Localizor(language, endonym) {
  this.language = language;
  this.endonym = endonym;
}
Localizor.prototype.native = function native() {
  return "I speak " + this.endonym;
};
Localizor.prototype.english = function () {
  return "I speak " + this.language;
};


function French (language, endonym) {
  Localizor.apply(this, arguments);
}
French.prototype = new Localizor();
French.prototype.native = function french() {
  return "Je parle " + this.endonym;
};


function Thai (language, endonym) {
  Localizor.apply(this, arguments);
}
Thai.prototype = new Localizor();
Thai.prototype.native = function thai() {
  return "พูดภาษา" + this.endonym;
};

这样,您可以调用正确的构造函数并取回相应的对象。您还可以调用“基类”构造函数并附加languageandendonym属性,而无需复制和粘贴该代码。您也不需要像当前所做的那样检查类型,因为您正在声明它们您希望它们如何工作。最后,如果你愿意,你可以让你的Localizor函数接受第三个参数,那就是字符串“I Speak”或其他任何东西,然后你就不需要多个对象了。

是一个你可以玩弄的游乐场。

服务器端或客户端,我认为这是一种更好的方法。

于 2013-09-01T13:30:11.363 回答