原型编程中对象和原型有什么区别?

发布于 2024-12-06 11:19:29 字数 846 浏览 2 评论 0原文

我试图理解创建和使用对象的“JavaScript 方式”,但我认为我对对象和原型产生了误解。

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

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

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

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

或者如果我应该创建一种类并使用 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 方面有更多的经验,后一种方法对我来说感觉更舒服(也许这就是足够的理由)。但我觉得原型继承的精神更多的是在第一个选择中。

哪种方法更符合原型编程的“精神”?或者我完全没有抓住要点?

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.

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';

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;

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裸钻 2024-12-13 11:19:29

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

当您这样做时:

var obj = Object.create( some_object );

...您是说您希望 obj 尝试从 some_object 获取属性,而 obj 上不存在这些属性代码>.

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

如果主对象具有与原型对象上存在的属性相同的属性,则该属性将隐藏原型的该属性。例如,您在 Dog 的属性上设置的 null 值。

如果您这样做:

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

...您现在正在遮蔽 Dog 上的 color 属性,因此您将不再获得 null。您没有以任何方式更改 Dog,因此,如果我创建另一个对象:

var colorless_dog = Object.create(Dog);

...这个对象仍会从以下位置获取 null 值 :访问 color 属性时的原型链。

colorless_dog.color;  // null

...直到你遮蔽它:

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

所以给出你的例子:

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

...它看起来像这样:

              // 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"
                                       }

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

When you do:

var obj = Object.create( some_object );

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

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.

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.

If you do:

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

...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);

...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"
                                       }
书间行客 2024-12-13 11:19:29

原型只是一个对象。

它是另一个对象用作其原型的任何对象。

A prototype is just an object.

It's any object that another object uses as it's prototype.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文