javascript - Object.prototype.toString.call()是不是无法区分基本包装类型

查看:69
本文介绍了javascript - Object.prototype.toString.call()是不是无法区分基本包装类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

基本类型值不是变量,基本包装类型的值是一个变量

var num1 = 123;
var num2 = new Number(123);

typeof num1; //'number'
typeof num2; //'object'

Object.prototype.toString.call(num1); 
"[object Number]"
Object.prototype.toString.call(num2);
"[object Number]"


var arr = new Array();
Object.prototype.toString.call(arr);
"[object Array]"

用Object.prototype.toString.call()方法可以去准确地判断一个非自定义的数据类型,但是这里num1和num2应该是不相同的两种类型为什么返回的是一样的结果,还有这个Number具体是什么意思呢,是基本数据类型中的Number类型呢还是引用类型中的Number类型呢?

解决方案

看规范 19.1.3.6

When the toString method is called, the following steps are taken:

  1. If the this value is undefined, return "[object Undefined]".

  2. If the this value is null, return "[object Null]".

  3. Let O be ToObject(this value).

  4. ...

会自动包装

这篇关于javascript - Object.prototype.toString.call()是不是无法区分基本包装类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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