原型编程中的对象和原型之间有什么区别? [英] What is the difference between an object and a prototype in prototypal programming?

查看:231
本文介绍了原型编程中的对象和原型之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解创建和使用对象的"JavaScript方式",我认为我对一个对象和一个原型有误解.

I'm trying to understand the "JavaScript way" of creating and using objects and I think I'm running into a misunderstanding of an object and a prototype.

在我开始的一个新项目中,我决定尝试原型继承.我很困惑,是否意味着我应该只创建要使用的对象,然后使用Object.create()基于该对象创建其他对象,例如:

In a new project I've started I've decided to try out prototypal inheritance. I'm confused if this means I should just create an object that I intend to use and then create other objects based on that using Object.create() such as:

var labrador = {
   color: 'golden',
   sheds: true,

   fetch: function()
   {
      // magic
   }
};

var jindo = Object.create(dog);
jindo.color = 'white';

或者如果我应该创建一种类,并使用Object.create()创建该类的实例.

Or if I should create a kind of class and that create instances of that using Object.create().

var Dog = { // Is this class-like thing a prototype?
   color: null,
   sheds: null,

   fetch: function()
   {
      // magic
   }
};

var labrador = Object.create(Dog);
labrador.color = 'golden';
labrador.sheds = true;

var jindo = Object.create(Dog);
jindo.color = 'white';
jindo.sheds = true;

对于基于类的OOP有更多的经验,后一种方法对我来说更舒服(也许这是足够的理由).但是我觉得原型继承的精神更多地在第一选择中.

Having much more experience in Class-based OOP the latter method feels more comfortable to me (and maybe that's reason enough). But I feel like the spirit of prototypal inheritance is more in the first option.

哪种方法更适合原型编程的精神"?还是我完全忘记了要点?

Which method is more in the "spirit" of prototypal programming? Or am I completely missing the point?

推荐答案

prototype只是对象具有隐式引用的另一个对象.

The prototype is just another object to which an object has an implicit reference.

当您这样做:

var obj = Object.create( some_object );

...您是说要obj尝试从some_object获取属性,而这些属性在obj上不存在.

...you're saying that you want obj to try to fetch properties from some_object, when they don't exist on obj.

这样,您的第二个示例将更接近您的使用方式.使用Object.create(Dog)创建的每个对象都将在其原型链中包含该Dog对象.因此,如果对Dog进行更改,则更改将反映在链中所有具有Dog的对象中.

As such, your second example would be closer to the way you'd use it. Every object that is created using Object.create(Dog) will have in its prototype chain, that Dog object. So if you make a change to Dog, the change will be reflected across all the objects that have Dog in the chain.

如果主对象具有与原型对象相同的属性,则该属性与原型的属性 shadowing 相同.例如,您可以在Dog的属性上设置的null值.

If the main object has the same property as exists on the prototype object, that property is shadowing that property of the prototype. An example of that would be the null values you set on properties of Dog.

如果您这样做:

var lab = Object.create(Dog);
lab.color = 'golden';

...您现在在Dogcolor属性上添加了阴影,因此您将不再获得null.您以任何方式更改Dog,所以如果我创建另一个对象:

...you're now shadowing the color property on Dog, so you'll no longer get null. You're not changing Dog in any way, so if I create another object:

var colorless_dog = Object.create(Dog);

...当访问color属性时,该对象仍将从原型链中获取null值.

...this one will still get the null value from the prototype chain when accessing the color property.

colorless_dog.color;  // null

...直到您将其遮盖:

...until you shadow it:

colorless_dog.color = 'blue';
colorless_dog.color;  // 'blue'

因此,以您的示例为例:

So given your example:

var lab = Object.create(Dog);
lab.color = 'golden';
lab.sheds = true;

...看起来像这样:

...it looks something like this:

              // labrador              // Dog
lab.color---> color:'golden'           color:null
lab.sheds---> sheds:true               sheds:null

lab.fetch()--------------------------> fetch: function() {
                                          alert( this.color ); // 'golden'
                                          // "this" is a reference to the
                                          //    "lab" object, instead of "Dog"
                                       }

这篇关于原型编程中的对象和原型之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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