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

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

问题描述

Kyle Simpson的OLOO(对象链接到其他对象)模式与原型设计模式有何不同?除了专门指出链接(原型的行为),并澄清说,在这里发生的复制(类的行为),他的模式究竟介绍了什么呢?



这是他的书你不知道JS:这个&对象原型的凯尔模式的一个例子:

  var Foo = {
init:function(who){
this.me = who;
},
识别:function(){
返回我是+ 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(); // alert:你好,我是b1。
b2.speak(); // alert:你好,我是b2。



模式引入?


OLOO原型链是原样的,不需要在其他(IMO混淆)语义层次上获得链接



所以,这两个片段有完全相同的结果,但是在不同的地方。



构造函数形式:

 函数Foo(){} 
Foo.prototype.y = 11;

函数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.prototype BarObj ),它又连接到第三个对象( Foo.prototype FooObj )。



代码段之间的关系和委派是相同的。代码段之间的内存使用情况相同。通过 x1000 等创建许多孩子(也称为 x1 的许多对象)的能力是相同的片段。代理人的表现( x.y x.z )在代码段之间是相同的。使用OLOO,对象创建性能 较慢,但理性检查显示,性能较慢并不是一个问题。



我认为OLOO提供的内容是简单地表达对象并直接链接它们,而不是通过构造函数/ 新的机制间接链接它们。后者假装是关于类,但真正只是表达授权的一个可怕的语法(侧注:,所以是ES6 语法!)。 / p>

OLOO只是切断了中间人。



这是另一个比较 class vs 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?

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 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.

Constructor Form:

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 Form:

var FooObj = { y: 11 };

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

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

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).

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.

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 is just cutting out the middle-man.

Here's another comparison of class vs OLOO.

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

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