简体   繁体   English

ECMAScript 6是否鼓励在JavaScript中使用经典继承?

[英]Does ECMAScript 6 encourage the use of classical inheritance in JavaScript?

I've come across many proponents of true prototypical inheritance in JavaScript (as opposed to constructor inheritance). 我在JavaScript中遇到过许多真正原型继承的支持者 (而不是构造函数继承)。 They suggest the new keyword shouldn't be used as it prevents the use of functional programming in the language. 他们建议不要使用new关键字,因为它阻止在语言中使用函数式编程。

Does ES6 encourage the use of classical inheritance? ES6是否鼓励使用经典继承? Considering the fact that it introduces the keyword class (called with the new keyword). 考虑到它引入了关键字class (使用new关键字调用)。

The article is outdated. 这篇文章已经过时了。 The problem of not being able to use .apply() with new is solved as of ECMAScript 6. You can now use spread syntax: new Person(...args) . 不能够使用的问题.apply()new解决作为ECMAScript的6.您现在可以使用传播语法: new Person(...args)

The new class syntax enhances prototypal inheritance by providing better code organization, neater syntax and some capabilities that were previously not possible. class语法通过提供更好的代码组织,更整洁的语法和以前不可能的一些功能来增强原型继承。 Even though it uses the keyword class , it has nothing to do with classical inheritance as found in Java, C++, etc.. 即使它使用关键字class ,它也与Java,C ++等中的经典继承无关。

No matter what you decide to use, endeavor to write clear, understandable, maintainable code, and you'll do well. 无论你决定使用什么,努力编写清晰,易懂,可维护的代码,你就会做得很好。

There are many patterns to achieve inheritance in JS. 在JS中有许多模式可以实现继承。 ES6 classes are just syntactic sugar for one of them. ES6课程只是其中之一的语法糖。

ECMAScript 6 is still JavaScript and doesn't encourage any particular paradigm. ECMAScript 6仍然是JavaScript,并不鼓励任何特定的范例。 However, there are practical considerations that are suggested by ES6 classes, which are idiomatic solution for inheritance in ES6. 但是,ES6类提出了一些实际的考虑因素,它们是ES6中继承的惯用解决方案。

When ES6 classes are inherited by non-ES6 classes, bad things may happen: 当ES6类由非ES6类继承时,可能会发生错误:

class Es6Class {}

function Es5Class() {
  var _this = Object.create(Es6Class.prototype);
  _this = Es6Class.apply(_this, arguments);
  return _this;
}

This is natural to call a constructor with call or apply and existing context on construction (this is roughly what happens when ES6 classes are transpiled with Babel or Typescript). 通过callapply以及构造时的现有上下文来调用构造函数是很自然的(这大致是在使用Babel或Typescript转换ES6类时会发生的情况)。 But ES6 classes have built-in protection against calling them without new , so Es5Class will fail. 但ES6类具有内置保护功能,可以在没有new情况下调用它们,因此Es5Class将失败。

Another problem appears when ES5 classes that implement random inheritance recipe are inherited by ES6 classes (as shown in this React question ): 当实现随机继承配方的ES5类由ES6类继承时,会出现另一个问题(如本React问题所示):

function Es5ClassWithShallowPrototypeChain() {
  var _this = {};
  _this = AnotherEs5Class.call(_this, arguments);
  _this.method = function () {};
}

class Es6Class {
  method() {}
}

In code above it's expected that Es6Class will override method but this won't happen, because method is instance method ( this.method ) in Es5ClassWithShallowPrototypeChain and prototype method ( this.__proto__method ) in Es6Class . 在上面的代码它的预期, Es6Class将覆盖method ,但是这不会发生,因为method是实例方法( this.method中) Es5ClassWithShallowPrototypeChain和原型方法( this.__proto__method中) Es6Class

A developer is free to choose any inheritance recipe he/she is comfortable but should always take into account how the recipe will affect third-party code. 开发人员可以自由选择他/她感到舒服的任何继承配方,但应始终考虑配方如何影响第三方代码。 While ES6 class inheritance implementation is idiomatic to JS and generally can be used as a rule of thumb in ES6 code. 虽然ES6类继承实现对于JS来说是惯用的,但通常可以在ES6代码中用作经验法则​​。

I would say yes, it does. 我会说是,确实如此。 As far as I know, it is still prototypical inheritance under the hood; 据我所知,它仍然是典型的继承; the class keyword formalizes a common design pattern. class关键字形式化了一个通用的设计模式。 This article covers this: 本文涵盖了这个:
Better JavaScript with ES6, Pt. 更好的JavaScript与ES6,Pt。 II: A Deep Dive into Classes II:深入学习课程

You can still do functional programming in JavaScript , but overall, the JS community seems to be moving away from that and more toward a classical object-oriented programming style. 您仍然可以在JavaScript中进行函数式编程 ,但总体而言,JS社区似乎正在逐渐远离这一点,而更多的是朝向经典的面向对象编程风格。

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

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