Constructor.prototype不在原型链中? [英] Constructor.prototype not in the prototype chain?

查看:73
本文介绍了Constructor.prototype不在原型链中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关:关于原型链,基元和对象的混淆

在Firebug控制台中:

in Firebug console :

a = 12
a.constructor.prototype.isPrototypeOf(a) // prints 'false'

我认为这应该打印 true

推荐答案

a = 12 创建一个原始数字,但不是与 Number 对象完全相同。出于属性访问的目的,隐式地将基元强制转换为对象。

a = 12 creates a primitive number, which is not quite the same as a Number object. Primitives are implicitly cast to objects for purposes of property access.

a = 12; //a is a primitive
b = new Number(12); //b is an object
a.constructor.prototype.isPrototypeOf(a); //false because a is primitive
b.constructor.prototype.isPrototypeOf(b); //true because b is an object

根据 ECMAScript规范


使用参数 V 调用isPrototypeOf 方法,执行以下步骤:

When the isPrototypeOf method is called with argument V, the following steps are taken:


  1. 如果 V 不是对象,则返回 false

  1. If V is not an object, return false.


原始数字严格来说不是对象。

primitive numbers are not, strictly speaking, objects.

这篇关于Constructor.prototype不在原型链中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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