Yegge的原型模式示例如何处理实例变量? [英] How does Yegge's prototype pattern example handle instance variables?

查看:110
本文介绍了Yegge的原型模式示例如何处理实例变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢Steve Yegge的原型模式示例和决定鞭打一个简单的概念证明例子。



然而,我并没有真正想到的事情。虽然动态地指定对象的行为非常好,但是对于Steve的有意见的精灵例如,我仍然试图找出处理实例变量的最佳方法。



例如,假设我有一个AwesomeDragon对象。然后我想制作一个AwesomeDragonImmuneToFire对象,所以我创建了一个AwesomeDragon的新孩子(AwesomeDragonImmuneToFire从AwesomeDragon继承属性),并将ImmuneToFire放在一个值为true的属性中。到现在为止还挺好。现在,我想发送我的AwesomeDragon对象来参观附近的农村。这将涉及更新AwesomeDragon的position属性。但是,当我这样做的时候,AwesomeDragonImmuneToFire也将起飞。



是创建对象时覆盖实例值的最佳解决方案。立即将AwesomeDragonImmuneToFire上的'position'值'put'放在'position'的当前'get'值中?

解决方案

它取决于您如何在系统中实际实现继承?



例如,在您描述的JavaScript版本中,原型 AwesomeDragonImmuneToFire 通常是 AwesomeDragon 实例,以及因为你一直在使用实例,所以对于任何特定的 AwesomeDragon 来说,这并不重要:

  function Dragon()
{
this.position =起点;
}

函数AwesomeDragon()
{
this.awesome = true;
}
AwesomeDragon.prototype = new Dragon();

函数AwesomeDragonImmuneToFire()
{
this.immuneToFire = true;
}
AwesomeDragonImmuneToFire.prototype = new AwesomeDragon();

>>> var awesome = new AwesomeDragon();
>>> var immune = new AwesomeDragonImmuneToFire();
>>> awesome.position =飞越村庄;
>>> immune.position;
起始点
>>>在这个例子中,没有类,所有的实例都只是这个例子。 Object 的实例,它们知道使用哪个函数来构造它们。 只是一点句法糖并且使用StudlyCaps作为构造函数只是一个用于与一起使用的函数的约定。



关键的是,每个对象都有一个原型对象链,如果您尝试访问对象本身不拥有的属性,则会被检查,就像Yegge对属性模式的描述一样。



https://developer.mozilla.org/en /Core_JavaScript_1.5_Guide/Details_of_the_Object_Model


I like Steve Yegge's Prototype Pattern example and decided to whip up a quick proof of concept example.

However, I didn't really think things through. While it is great for dynamically specifying the behaviour of objects and is an easy solution to Steve's opinionated elf example, I'm still trying to work out the best way to handle instance variables.

For instance, let's say I have an AwesomeDragon object. I then want to make an AwesomeDragonImmuneToFire object so I make a new child of the AwesomeDragon (AwesomeDragonImmuneToFire inherits properties from AwesomeDragon) and 'put' "ImmuneToFire" as a property with a value of 'true'. So far so good. Now let's say I want to send my AwesomeDragon object on a tour of nearby peasant villages. This will involve updating the 'position' property of AwesomeDragon. However, the moment I do this AwesomeDragonImmuneToFire will take off as well.

Is the best solution to override instance values upon object creation e.g. immediately 'put' the 'position' value on AwesomeDragonImmuneToFire to the current 'get' value of 'position'?

解决方案

Doesn't it depend how you actually implement the inheritance in your system?

For example, in a JavaScript version of what you describe, the prototype for AwesomeDragonImmuneToFire would normally be an instance of an AwesomeDragon, and since you'd always be working with instances, it wouldn't matter what you do to any particular AwesomeDragon:

function Dragon()
{
    this.position = "starting point";
}

function AwesomeDragon()
{
    this.awesome = true;
}
AwesomeDragon.prototype = new Dragon();

function AwesomeDragonImmuneToFire()
{
    this.immuneToFire = true;
}
AwesomeDragonImmuneToFire.prototype = new AwesomeDragon();

>>> var awesome = new AwesomeDragon();
>>> var immune = new AwesomeDragonImmuneToFire();
>>> awesome.position = "flying above village";
>>> immune.position;
"starting point"
>>> immune.awesome
true

In this example, there are no classes and all instances are just instances of Object which happen to know which function was used to construct them. new is just a bit of syntactic sugar and using StudlyCaps for constructor functions is just a convention for functions which are intended to be used with new.

The key thing is that each object has a chain of prototype objects, which is examined if you try to access an attribute which the object itself doesn't hold, as per Yegge's description of what the "Properties Pattern" is.

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Details_of_the_Object_Model

这篇关于Yegge的原型模式示例如何处理实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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