Kyle Simpson 的 OLOO 模式与原型设计模式 [英] Kyle Simpson's OLOO Pattern vs Prototype Design Pattern

查看:18
本文介绍了Kyle Simpson 的 OLOO 模式与原型设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Kyle Simpson 的OLOO(对象链接到其他对象)模式"与原型设计模式有什么不同吗?除了通过特别指示链接"(原型的行为)的东西来创造它并澄清这里没有复制"发生(类的行为)之外,他的模式到底引入了什么?

Does Kyle Simpson's "OLOO (Objects Linking to Other Objects) Pattern" differ in any way from the the Prototype design pattern? Other than coining it by something that specifically indicates "linking" (the behavior of prototypes) and clarifying that there's no to "copying" happening here (a behavior of classes), what exactly does his pattern introduce?

这里是 Kyle 模式的一个例子,来自他的书《你不知道的 JS:这个和对象原型》:

Here's an example of Kyle's pattern from his book, "You Don't Know JS: this & Object Prototypes":

var Foo = {
    init: function(who) {
        this.me = who;
    },
    identify: function() {
        return "I am " + this.me;
    }
};

var Bar = Object.create(Foo);

Bar.speak = function() {
    alert("Hello, " + this.identify() + ".");
};

var b1 = Object.create(Bar);
b1.init("b1");
var b2 = Object.create(Bar);
b2.init("b2");

b1.speak(); // alerts: "Hello, I am b1."
b2.speak(); // alerts: "Hello, I am b2."

推荐答案

他的模式到底介绍了什么?

what exactly does his pattern introduce?

OLOO 按原样包含原型链,无需在其他(IMO 混淆)语义上分层以获得链接.

OLOO embraces the prototype chain as-is, without needing to layer on other (IMO confusing) semantics to get the linkage.

因此,这两个片段具有完全相同的结果,但实现方式不同.

So, these two snippets have the EXACT same outcome, but get there differently.

构造函数形式:

function Foo() {}
Foo.prototype.y = 11;

function Bar() {}
Bar.prototype = Object.create(Foo.prototype);
Bar.prototype.z = 31;

var x = new Bar();
x.y + x.z;  // 42

OLOO 表单:

var FooObj = { y: 11 };

var BarObj = Object.create(FooObj);
BarObj.z = 31;

var x = Object.create(BarObj);
x.y + x.z;  // 42

在两个片段中,x 对象是 [[Prototype]] 链接到一个对象(Bar.prototypeBarObj),后者又链接到第三个对象(Foo.prototypeFooObj).

In both snippets, an x object is [[Prototype]]-linked to an object (Bar.prototype or BarObj), which in turn is linked to third object (Foo.prototype or FooObj).

片段之间的关系和委托是相同的.代码段之间的内存使用是相同的.创建许多子项"(也就是许多对象,如 x1x1000 等)的能力在代码段之间是相同的.代码段之间的委托(x.yx.z)的性能是相同的.使用 OLOO 的对象创建性能较慢,但健全性检查表明性能较慢实际上不是问题.

The relationships and delegation are identical between the snippets. The memory usage is identical between the snippets. The ability to create many "children" (aka, many objects like x1 through x1000, etc) is identical between the snippets. The performance of the delegation (x.y and x.z) is identical between the snippets. The object creation performance is slower with OLOO, but sanity checking that reveals that the slower performance is really not an issue.

我认为 OLOO 提供的是,仅表达对象并直接链接它们比通过构造函数/new 机制间接链接它们要简单得多.后者假装是关于类,但实际上只是表达委托的一种糟糕的语法(旁注: ES6 class 语法也是如此!)

What I argue OLOO offers is that it's much simpler to just express the objects and directly link them, than to indirectly link them through the constructor/new mechanisms. The latter pretends to be about classes but really is just a terrible syntax for expressing delegation (side note: so is ES6 class syntax!).

OLOO 只是切断了中间人.

这是 另一个对比 class 与 OLOO.

Here's another comparison of class vs OLOO.

这篇关于Kyle Simpson 的 OLOO 模式与原型设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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