原型编程中对象和原型有什么区别?
我试图理解创建和使用对象的“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
prototype
只是一个对象具有隐式引用的另一个对象。当您这样做时:
...您是说您希望 obj 尝试从
some_object
获取属性,而obj
上不存在这些属性代码>.因此,您的第二个示例将更接近您的使用方式。使用
Object.create(Dog)
创建的每个对象都会在其原型链中包含该Dog
对象。因此,如果您对Dog
进行更改,则更改将反映在链中具有Dog
的所有对象上。如果主对象具有与原型对象上存在的属性相同的属性,则该属性将隐藏原型的该属性。例如,您在
Dog
的属性上设置的null
值。如果您这样做:
...您现在正在遮蔽
Dog
上的color
属性,因此您将不再获得null
。您没有以任何方式更改Dog
,因此,如果我创建另一个对象:...这个对象仍会从以下位置获取
null
值 :访问color
属性时的原型链。...直到你遮蔽它:
所以给出你的例子:
...它看起来像这样:
The
prototype
is just another object to which an object has an implicit reference.When you do:
...you're saying that you want
obj
to try to fetch properties fromsome_object
, when they don't exist onobj
.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, thatDog
object. So if you make a change toDog
, the change will be reflected across all the objects that haveDog
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 ofDog
.If you do:
...you're now shadowing the
color
property onDog
, so you'll no longer getnull
. You're not changingDog
in any way, so if I create another object:...this one will still get the
null
value from the prototype chain when accessing thecolor
property....until you shadow it:
So given your example:
...it looks something like this:
原型只是一个对象。
它是另一个对象用作其原型的任何对象。
A prototype is just an object.
It's any object that another object uses as it's prototype.