函数和新函数之间的JavaScript差异 [英] JavaScript difference between function and new function

查看:168
本文介绍了函数和新函数之间的JavaScript差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下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)实际检查 PersonY 的原型。你会看到它有一个原型属性(我在Chrome中看到它是 __ 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差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆