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

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

问题描述

我喜欢Steve Yegge的原型模式示例和决定提出一个快速的概念证明示例.

但是,我并没有真正考虑到问题.尽管它对于动态指定对象的行为非常有用,并且它是解决Steve 带蛋白的精灵例如,我仍在尝试找出处理实例变量的最佳方法.

例如,假设我有一个AwesomeDragon对象.然后,我想制作一个AwesomeDragonImmuneToFire对象,以便制作一个AwesomeDragon的新子对象(AwesomeDragonImmuneToFire继承了AwesomeDragon的属性),并将"ImmuneToFire"设置为值为"true"的属性.到现在为止还挺好.现在,假设我要在附近的农民村庄游览中发送我的AwesomeDragon对象.这将涉及更新AwesomeDragon的'position'属性.但是,当我这样做时,AwesomeDragonImmuneToFire也将起飞.

是在创建对象时覆盖实例值的最佳解决方案,例如立即将AwesomeDragonImmuneToFire上的位置"值放置"到当前位置"的获取"值?

解决方案

这不取决于您实际上如何在系统中实现继承吗?

例如,在您所描述的JavaScript版本中, AwesomeDragonImmuneToFire prototype 通常是 instance AwesomeDragon ,并且由于您一直都在使用实例,因此对任何特定的 AwesomeDragon

做什么都无所谓.

 函数Dragon(){this.position =起点";}函数AwesomeDragon(){this.awesome = true;}AwesomeDragon.prototype = new Dragon();函数AwesomeDragonImmuneToFire(){this.immuneToFire = true;}AwesomeDragonImmuneToFire.prototype = new AwesomeDragon();>>>var awesome = new AwesomeDragon();>>>varimmun = new AwesomeDragonImmuneToFire();>>>awesome.position =在村庄上空飞行";>>>免疫位置初始点">>>真棒真的 

在此示例中,没有类,所有实例只是 Object 的实例,它们碰巧知道使用了哪个函数来构造它们. new 只是一点语法糖,并使用StudlyCaps构造函数只是要与 new 一起使用的函数的约定.

关键是每个对象都有一串原型对象,根据Yegge对属性模式"的描述,将检查是否尝试访问该对象本身不具有的属性./p>

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天全站免登陆