[英]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
。
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
属性来自“旧”原型 - 一个与函数一起初始化并具有隐藏的“构造函数”属性的对象 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
函数。 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. 在第一种情况下,您只是将
Foo
的prototype
属性设置为空对象。 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.