Object.toString和Object.prototype.toString之间有所不同 [英] different between Object.toString and Object.prototype.toString

查看:385
本文介绍了Object.toString和Object.prototype.toString之间有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用 Object.prototype.toString.call(foo)来检测对象类(foo的类型),它运行良好。

We can use Object.prototype.toString.call(foo) to detect object class (the type of foo), and it works well.

但为什么 Object.toString.call({}) throw TypeError:Function.prototype.toString is不是通用的

But why does Object.toString.call({}) throw TypeError: Function.prototype.toString is not generic?

不是 Object.toString 从<$ c $继承C> Object.prototype中?

推荐答案


Object.toString是否继承自Object.prototype

Doesn't Object.toString inherit from Object.prototype

不。内置的 Object 构造函数是一个函数(像所有本机构造函数一样),所以它继承自 Function.prototype (即它的私有 [[Prototype]] 属性引用 Function.prototype )在它自己的原型属性之前。

No. The built–in Object constructor is a Function (like all native constructors), so it inherits from Function.prototype (i.e. its private [[Prototype]] property references Function.prototype) before its own prototype property.

它的原型链是:

Object[[Prototype]] -> Function.prototype -> Object.prototype -> null

所以 Function.prototype.toString 掩盖 Object.prototype .toString

有点琐事:请注意,虽然 Function.prototype 是一个函数,但它不会继承自本身,但来自 Object.prototype

A bit of trivia: note that while Function.prototype is a function, it doesn't inherit from itself but from Object.prototype.

这篇关于Object.toString和Object.prototype.toString之间有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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