您将 Mixin/Traits 系统的对象级别等效项称为什么,它有模式名称吗?
我之前询问过 Mixin 是什么,并且已经开始了解该模式的含义。 但这让我想知道是否有一个通用的模式名称可以在对象级别(而不是类级别)执行诸如 Mixins 之类的操作。
伪代码(用某种不存在的语言):
Class MyClass
{
function foo()
{
print("foo")
}
}
function bar()
{
print("bar")
}
object = MyClass.new()
object.xxxx(bar)
object.bar() #output: bar
我知道这样的事情可以用多种语言以一种或另一种方式完成,但我想知道 xxxx 代表的功能的“标准”名称是什么,以及什么是该模式的名称(如果有)。
谢谢!
编辑:扩展 finnsson 的答案我想类似的事情可能是另一种情况:
object.xxxx(OtherClass)
object.otherfoo()
连接是否合适?
引用:“串联:在纯原型下,也称为串联原型......”-维基百科
I previously asked about what Mixins were, and have begun to get the gist of what the pattern means. But it got me wondering if there is a common pattern name for doing something like Mixins at an Object level as opposed to the Class level.
Pseudo code (in some non existent language):
Class MyClass
{
function foo()
{
print("foo")
}
}
function bar()
{
print("bar")
}
object = MyClass.new()
object.xxxx(bar)
object.bar() #output: bar
I know stuff like this can be done in several languages, in one way or another, but I'm wondering what would be the "standard" name for the functionality xxxx represents, and what is the name for this pattern, if there is one.
Thanks!
Edit: Expanding on finnsson's answer I guess something like this might be another case of this would be:
object.xxxx(OtherClass)
object.otherfoo()
Would concatenate be appropriate?
Quote: "Concatenation: Under pure prototyping, which is also referred to as concatenative prototypes..." -wikipedia
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在基于原型的编程语言中很常见。 我相信它在 ruby 中被称为“导入”,但自从我上次编写 ruby 以来已经有一段时间了,所以我不确定。
在 js/ruby 中,您可以编写它
,但它不是真正的模式,因为它只是一个赋值(o.bar = bar),在基于原型的语言中非常有意义。 我猜你的例子中的 xxxx 可以被称为原型或类似的东西(参见 http://en.wikipedia .org/wiki/Prototype-based_programming,其中一种语言将此称为原型)。
This is common in prototype-based programming languages. I belive it's called "import" in ruby but it's some time since I last programmed ruby so I'm not sure.
In js/ruby you would write
and than it's no real pattern, since it's just an assignment (o.bar = bar) making perfect sense in a prototype-based language. I guess xxxx in your example could be called prototype or something similar (see http://en.wikipedia.org/wiki/Prototype-based_programming where a language calles this proto).