下面的 JavaScript 代码让我很困惑。谁能帮我理解一下。为什么 PersonY 没有原型属性。
PersonX = function(){};
PersonY = new function(){};
alert(PersonX.prototype);
alert(PersonY.prototype);
PersonX = function(){};
将对匿名函数的引用放入PersonX
. PersonX
指向一个函数。
PersonY = new function(){};
将对匿名构造函数的新构造实例的引用放入PersonY
. PersonY
指向一个对象。
关于原型,PersonY
有一个。但是,由于构造函数没有附加任何属性和方法before http://jsfiddle.net/tcrKW/1/ nor after http://jsfiddle.net/tcrKW/实例化,它有一个空白原型*.
你实际上可以检查PersonY
的原型通过做console.log(PersonY)
。你会看到它有一个原型属性(我将其视为__proto__
在 Chrome 中)这是“空白”。但它有两个隐藏属性,constructor
这是创建对象的构造函数,另一个是__proto__
这将引导您到达下一个“链环”,即Object
object.
*Not really blank since prototype is a chain. This prototype level may be blank, but the next higher prototype may have, or in this case, does have properties and methods.
Object prototype -> Constructor prototype -> Your Instance will have:
- toString() - blank - toString()
- hasOwnProperty() - hasOwnProperty()
- and more... - and more...
- ...but nothing from Constructor
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)