基于原型的面向对象与基于类的面向对象相比有哪些优势? [英] What are the advantages that prototype based OO has over class based OO?

查看:40
本文介绍了基于原型的面向对象与基于类的面向对象相比有哪些优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么基于类的 OO 比基于原型的 OO 如此受欢迎?他们在学校教后者吗?尽管 Javascript 是基于原型的,但大多数人主要是在功能上使用它,或者通过试图模拟基于类的系统的框架来使用它.

Why is class based OO so popular instead of prototype based OO? Do they teach the latter in schools? Though Javascript is prototype based, most people use it mostly functionally, or via frameworks that try to emulate a class based system.

我知道 Sun 对 Self 进行了一些研究 - 是否还有其他关于基于原型的 oo?最好是可以自学的东西.

I know that Sun has had some research on Self - is there any other source of knowledge on prototype based oo? preferably something that is accessible for self learned.

我找到了一本包含已发表论文的书:基于原型编程:概念、语言和应用

I found a book that contains published papers: Prototype-Based Programming: Concepts, Languages and Applications

有人读过吗?

--

所以我为给我最多的答案提供了赏金.尽管如此,我还是不太满意.我希望听到更多技术性的答案.也许我没有很好地解释自己.

So I gave the bounty for the answer that gave me most. Still, I'm not really satisfied. I would have liked to hear much more techical answers. Maybe I didn't explain myself well.

推荐答案

原型继承的优势在于它可能允许以一种直接的方式进行花哨的元编程,因为原型链很容易操作.这是一个相当学术的优势,因为元编程在 99% 的情况下都是错误的答案.例如,您可以拥有一个带有特殊 DSL 的 Javascript Key-Value Observer 样式数据操作层,可以在离线时通过原型交换在本地 SQLite 支持和在线时基于 REST 的服务器存储之间透明地切换.我不确定这是最好的方法,但这是我这么晚能想到的最好方法.这不是您通常希望在项目代码中做的那种事情,因为一旦您开始在多个层上运行这种间接方式,就很难调试,但是将它保存在库中时也不错.

The advantage of prototypal inheritance is that it potentially allows fancy metaprogramming in a straightforward way because the prototype chain is easily manipulated. This is a rather academic advantage because metaprogramming is the wrong answer 99% of the time. As an example, you could have a Javascript Key-Value Observer style data manipulation layer with a special DSL that transparently switched between a local SQLite backing when offline and a REST based server store when online via prototype swapping. I'm not sure it's the best way to do this, but it's the best I can come up with this late. It's not the sort of thing you generally want to do in project code since this sort of indirection is hell to debug once you start getting it going on multiple layers, but it's not bad when you keep it in a library.

另一个不太有用的优点是它允许您设计自己的类系统.我说不太有用,因为或多或少所有 javascript 库都有自己稍微不兼容的方法来将类"放在一起.

Another less helpful advantage is that it allows you to design your own class system. I say less helpful because more or less all javascript libraries have their own slightly incompatible approach to how 'classes' are put together.

有很多人将继承模型与该模型中实现的语言混合在一起.JavaScript 是动态的、弱类型的,因此难以使用这一事实与它是一种原型语言无关.

There are a lot of people replying who are mixing the inheritance model with the languages implemented in that model. The fact that javascript is dynamic and weakly typed and thus hard to tool has nothing to do with it being a prototypal language.

这篇关于基于原型的面向对象与基于类的面向对象相比有哪些优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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