除了模仿经典类系统之外,JavaScript原型系统还能做什么? [英] What can the JavaScript prototype system do beyond mimicking a classical class system?

查看:49
本文介绍了除了模仿经典类系统之外,JavaScript原型系统还能做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原型系统看起来比传统的类系统灵活得多,但是人们似乎对模仿传统的类系统的所谓最佳实践"感到满意:

The prototype system looks much more flexible than the traditional class system, but people seem to feel content with the so-called "best practices", which mimic the traditional class system:

function foo() {
  // define instance properties here
}

foo.prototype.method = //define instance method here

new foo()

原型系统还必须具备其他所有灵活性.

There must be other things that a prototypal system can do with all the flexibility.

在模仿类之外还有原型系统的用途吗?原型可以做哪些事情,哪些类不能做,或者没有呢?

Are there uses for a prototypal system outside of mimicking classes? What kinds of things can prototypes do which classes cannot, or are there none?

推荐答案

原型系统提供了元编程,通过标准对象实现继承.当然,这主要用于表达实例类的既定概念和简单概念,但是没有类作为需要特定语法来创建它们的语言级不可变结构.通过使用普通对象,您可以对对象做的所有事情(并且您可以做所有事情),现在可以对对象进行分类".-这就是您所说的灵活性.

The prototype system offers a captivating model of metaprogramming, by implementing inheritance via standard objects. Of course, this is mostly used to express the established and simple concept of classes of instances, but without classes as language-level immutable structures that need specific syntax to create them. By using plain objects, all you can do to objects (and you can do everything) you can now do to "classes" - this is the flexibility you talk of.

然后,仅使用给定的JavaScript对象突变功能,就可以通过编程方式广泛地使用这种灵活性来扩展和更改类:

This flexibility is then used a lot to extend and alter classes programmatically, using only the given object-mutation capabilities of JavaScript:

  • 用于多重继承的混合素和特征
  • 在实例化从其继承的对象之后,可以修改
  • 原型
  • 高阶函数和方法修饰符可轻松用于创建原型
  • mixins and traits for multiple inheritance
  • prototypes can be modified after objects that inherit from them have been instantiated
  • higher-order functions and method decorators can be used easily in the creation of prototypes

当然,原型模型本身比仅实现类更强大.这些功能很少使用,因为类概念非常有用且广泛使用,因此原型继承的实际功能并不为人所知(并且在JS引擎中未得到充分优化:-/)

Of course, the prototype model itself is more powerful than to just implement classes. These features are used rather seldom, as the class concept is very useful and widespread, so the actual powers of prototype inheritance are not well-known (and not well-optimised in JS engines :-/)

一些软件工程模式可以直接与对象一起实现.示例包括具有属性的 flyweight模式,一个

a few software engineering patterns can be implemented directly with objects. Examples are the flyweight pattern with properties, a chain of responsibilities including dynamic chains, oh, and of course the prototype pattern.

最后一个很好的例子是带有默认值的选项对象.每个人都使用

A good example for the last one would be option objects with defaults. Everyone creates them using

  var myOptions = extend({}, defaultOptions, optionArgument);

但更动态的方法是使用

  var myOptions = extend(Object.create(defaultOptions), optionArgument);

这篇关于除了模仿经典类系统之外,JavaScript原型系统还能做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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