简体   繁体   English

原型javascript

[英]prototype javascript

Cannot understand: 不能理解:

function Rabbit() { }
Rabbit.prototype = { eats: true };
var rabbit = new Rabbit();
Rabbit.prototype = {};
alert(rabbit.eats); //true

And

function Rabbit() { }
Rabbit.prototype = { eats: true };
var rabbit = new Rabbit();
Rabbit.prototype.eats = false;
alert(rabbit.eats); // false

Why? 为什么?
Thanks everyone for help, I get it after "In your first example, you change Rabbit.prototype to point to an entirely new, different, object. So now Rabbit.prototype and rabbit.[[Proto]] no longer point to the same object" (c) TJ Crowder 感谢大家的帮助,我得到它“在你的第一个例子中,你改变Rabbit.prototype指向一个全新的,不同的对象。所以现在Rabbit.prototype和兔子。[[Proto]]不再指向相同的对象“(c)TJ Crowder

Because the prototype behind the rabbit instance is assigned (from Rabbit.prototype ) when it's created via new Rabbit , and in the first example you replace Rabbit.prototype with an entirely new object, which has no effect on the rabbit instance. 因为rabbit实例背后的原型是通过new Rabbit创建的(来自Rabbit.prototype ),在第一个例子中,你用一个全新的对象替换 Rabbit.prototype ,这对rabbit实例没有影响。 In the second example, you just modify the existing object, which both Rabbit.prototype and the prototype behind rabbit are pointing to, so since they're pointing to the same object, you see the change regardless of which reference you follow. 在第二个例子中,你只需要修改现有的对象,这既Rabbit.prototype和背后的原型rabbit都指向,这样以来他们指向同一个对象,你看到的变化,无论其参考你跟随。

Let's walk through var rabbit = new Rabbit(); 让我们来看看var rabbit = new Rabbit(); (leaving out some irrelevant details): (遗漏一些不相关的细节):

  1. A new, blank object gets created. 创建一个新的空白对象。

  2. Its underlying prototype, usually called [[Proto]] , is set to Rabbit.prototype . 它的底层原型,通常称为[[Proto]] ,设置为Rabbit.prototype (I say "usually called" because that's what the specification calls it. You can't access it by that name directly. You can find out what it is for a given object, though, in an ES5-enabled environment via Object.getPrototypeOf . Some JavaScript engines also make it available via a non-standard property actually called [in code] __proto__ .) (我说“通常称为”,因为这是规范所称的。你不能直接通过该名称访问它。你可以通过Object.getPrototypeOf在支持ES5的环境中找到给定对象的内容。一些JavaScript引擎也通过一个实际上称为[in code] __proto__的非标准属性使它可用。)

  3. That new object is returned and assigned to rabbit . 该新对象被返回并分配给rabbit

Now, we have two objects pointing to the prototype object: Rabbit.prototype , and rabbit.[[Proto]] . 现在,我们有两个指向原型对象的对象: Rabbit.prototyperabbit.[[Proto]]

In your first example, you change Rabbit.prototype to point to an entirely new, different, object. 在第一个示例中,您将Rabbit.prototype更改为指向一个全新的,不同的对象。 So now Rabbit.prototype and rabbit.[[Proto]] no longer point to the same object. 所以现在Rabbit.prototyperabbit.[[Proto]]不再指向同一个对象。

In your second example, you just change the value of that shared object's eats property, so naturally that change is visible regardless of which reference to the object you follow. 在第二个示例中,您只需更改该共享对象的eats属性的值,因此无论您对所关注对象的引用如何,这种更改都是可见的。

It's basically the same as this: 它与此基本相同:

var a = {eats: true};
var b = a;
a = {eats: false};
console.log(a.eats); // false
console.log(b.eats); // true

and

var a = {eats: true};
var b = a;
a.eats = false;
console.log(a.eats); // false
console.log(b.eats); // false
Rabbit.prototype = { eats: true };

is equivalent to 相当于

Rabbit.prototype = {};
Rabbit.prototype.eats = true;

And

Rabbit.prototype.eats = false;

just makes Rabbit.prototype.eats false (thus voiding the precedent assignation). 只是使Rabbit.prototype.eats false (因此排除先前的分配)。

And as the js engine, when looking for a property, looks for the one of its prototype (and so on) if it doesn't find it locally, it makes Rabbit.eats evaluate to false. 并且作为js引擎,在查找属性时,查找其原型之一(依此类推)如果它在本地找不到它,则会使Rabbit.eats评估为false。

In the first case, the constructed object remains linked to the original prototype object, which of course still has the "eats" property. 在第一种情况下,构造的对象仍然链接到原始原型对象,当然它仍具有“吃”属性。

In the second case, you're not introducing a new prototype, so changing the value of its "eats" property is visible via the instance. 在第二种情况下,您没有引入新的原型,因此通过实例可以看到更改其“吃”属性的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM