简体   繁体   English

JavaScript的:原型

[英]javascript:prototype

when the new object is constructed ,The object is set up to delegate any properties which haven't been explicitly set up to its constructor's prototype. 构造新对象时,该对象被设置为委托任何尚未明确设置为其构造函数原型的属性。 That means that we can change the prototype later, and still see the changes in the instance. 这意味着我们可以稍后更改原型,并仍然可以看到实例中的更改。

first: 第一:

 function Foo(){}
 foo=new Foo();
 Foo.prototype={};
 foo.constructor==Foo//true.why is this happening since construtor prototype is empty object 

so the statement are not working as per the definition.right or wrong? 所以声明不符合定义。是对还是错? but if i do this than result is different 但如果我这样做,结果就不同了

second: 第二:

 function Foo(){}
  Foo.prototype={};
 foo=new Foo();

 foo.constructor==Foo//false as aspected

again third: 再次第三:

  function Foo(){}
  Foo.prototype={};
   Foo.prototype.name="Maizere";
 foo=new Foo();

 foo.name==Maizere//true.The definition at the top is applying here,if so why the definition not working in the first: example

plz help with simple english.im really getting headache. PLZ帮助简单的english.im真的让人头疼。

why is this happening 为什么会发生这种情况

The new operator sets the inheritance of the instance to the object that the constructor's prototype property currently points to. new运算符将实例的继承设置为构造函数的prototype属性当前指向的对象。 So when you do 所以,当你这样做

function Foo() {}
var foo = new Foo;

it is irrelevant if someone assigns to Foo.prototype after that - it will not change foo . 如果有人在那之后分配给Foo.prototype则无关紧要 - 它不会改变foo

  • first: The inherited constructor property you are getting comes from the "old" prototype - an object that is initialized together with the function and having that hidden "constructor" property 第一:你得到的继承的constructor属性来自“旧”原型 - 一个与函数一起初始化并具有隐藏的“构造函数”属性的对象
  • second: You are overwriting the prototype property with an empty object, and your foo (created after overwriting) inherits from that. 第二:你用一个空对象覆盖prototype属性,你的foo (覆盖后创建)继承了它。 And since empty objects inherit from Object.prototype , the constructor property now points to the Object function. 由于空对象继承自Object.prototype ,因此constructor属性现在指向Object函数。
  • third: Again, you create foo after overwriting the prototype . 第三:再次,你在覆盖prototype后创建了foo Since you assign a name property to that new object from which foo inherits from, you can access it on foo as well. 由于您将name属性分配给foo继承自的新对象,因此您也可以在foo上访问它。

When you write a function, it comes with a property named prototype - an object whose constructor property is set to the function itself. 当你编写一个函数时,它带有一个名为prototype的属性 - 一个对象,其constructor属性设置为函数本身。 As you may have known, JavaScript's object model is prototype based. 您可能已经知道,JavaScript的对象模型是基于原型的。 This makes the objects you create with that function inherit all the properties of the prototype of its constructor function (the one you invoke with new ) - so you should beware what prototype is the moment you create the object . 这使得使用该函数创建的对象继承了其构造函数prototype的所有属性(您使用new调用的那个) - 因此您应该注意创建对象时的 prototype

In your first case, you are just setting the prototype property of Foo to an empty object. 在第一种情况下,您只是将Fooprototype属性设置为空对象。 But the original (default) prototype is still referenced by foo . 但原始(默认) prototype仍由foo引用。 That explains why the last expression evaluates to true. 这解释了为什么最后一个表达式的计算结果为true。

By the time you create foo in your second case, prototype of Foo has already been set to an empty object. 当你在第二种情况下创建foo时, Foo prototype已经被设置为一个空对象。 So virtually foo inherits nothing from it. 所以foo几乎没有继承它。 The same principle applies to the third case. 同样的原则适用于第三种情况。

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

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