问题描述
以下JavaScript代码对我来说非常混乱。任何人都可以帮助我理解。为什么PersonY没有原型属性。
The following JavaScript code is very confusing to me. Could anyone help me understand. Why does PersonY not have prototype property.
PersonX = function(){};
PersonY = new function(){};
alert(PersonX.prototype);
alert(PersonY.prototype);
推荐答案
PersonX = function(){};
将对匿名函数的引用放入 PersonX
。 PersonX
指向一个函数。
Places a reference to an anonymous function into PersonX
. PersonX
points to a function.
PersonY = new function(){};
将新构造的匿名构造函数实例的引用放入 PersonY
。 PersonY
指向一个对象。
Places a reference to a newly constructed instance of an anonymous constructor function into PersonY
. PersonY
points to an object.
关于原型, PersonY
有一个。但是,由于没有属性和方法附加到构造函数或实例化后,它有一个空白原型 *。
Regarding the prototype, PersonY
has one. However, since there were no properties and methods attached to the constructor before nor after instantiation, it has a blank prototype*.
您可以通过 console.log(PersonY)实际检查
。你会看到它有一个原型属性(我在Chrome中看到它是 PersonY
的原型 __ proto __
),这是空白。但它有2个隐藏的属性,构造函数
这是构成该对象的构造函数,另一个 __ proto __
引导你到下一个链接,它将是对象
对象。
You can actually check PersonY
's prototype by doing console.log(PersonY)
. You will see that it has a prototype property (I see it as __proto__
in Chrome) which is "blank". But it has 2 hidden properties, constructor
which is the constructor function that made the object, and another __proto__
which leads you to the next "chain link" which would be the Object
object.
Object prototype -> Constructor prototype -> Your Instance will have:
- toString() - blank - toString()
- hasOwnProperty() - hasOwnProperty()
- and more... - and more...
- ...but nothing from Constructor
这篇关于函数和新函数之间的JavaScript差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!