源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JS中令人发指的valueOf方法介绍

  • 时间:2022-08-07 06:22 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JS中令人发指的valueOf方法介绍
彭老湿近期月报里提到了valueOf方法,兴致来了翻了下ECMA5里关于valueOf方法的介绍,如下: 15.2.4.4 Object.prototype.valueOf ( ) When the valueOf method is called, the following steps are taken: 1. Let O be the result of calling ToObject passing the this value as the argument. 2. If O is the result of calling the Object constructor with a host object (15.2.2.1), then a. Return either O or another value such as the host object originally passed to the constructor. The specific result that is returned is implementation-defined. 3. Return O. 规范里面的对于valueOf的解释很短,大致为:调用ToObject方法(一个抽象方法,后面会讲到),并将this的值作为参数传入。 [b]针对调用ToObject时传入的不同参数(this),返回值分别如[/b]下: 1、this为宿主对象时,返回值取决于浏览器的实现,即不同浏览器的返回可能不同(关于宿主对象,可参考http://www.w3school.com.cn/js/pro_js_object_types.asp) 2、this不是宿主对象,则返回ToObject(this)的值 
[b]参数类型  [/b] [b]返回结果[/b]
Undefined 抛出TypeError异常
Null 抛出TypeError异常
Number 创建一个Number对象,它内部的初始值为传入的参数值
String 创建一个String对象,它内部的初始值为传入的参数值
Boolean 创建一个Boolean对象,它内部的初始值为传入的参数值
Object 返回传入的参数(无转换)
根据Object.prototype.valueOf的定义,以及抽象方法ToObject的描述,可得下表 
[b]obj类型  [/b] [b]Object.prototype.valueOf.call(obj)返回结果[/b]
Undefined 抛出TypeError异常
Null 抛出TypeError异常
Number Number类型的对象,值等于obj
String String类型的对象,值等于obj
Boolean Boolean类型的对象,值等于obj
Object obj对象本身
举几个具体的例子:
[url=http://jb51.net/article/34190.htm]JS中不为人知的五种声明Number的方式[/url]》 [b]关于返回值的说明,ECMA5里面原文如下[/b]: Create a new Number object whose [[PrimitiveValue]] internal property is set to the value of the argument. See 15.7 for a description of Number objects. 按照这段文字的说明,似乎num.valueOf()返回的应该是个Number对象(非字面量声明的那种),但实际上:
[u]复制代码[/u] 代码如下:
var num = 123; var tmp = num.valueOf(); console.log(typeof tmp); //输出: 'number'
这是怎么回事呢?于是又仔细翻看了下,似乎有些接近真相了: 5.7.4.4 Number.prototype.valueOf ( ) Returns this Number value. The valueOf function is not generic; it throws a TypeError exception if its this value is not a Number or a Number object. Therefore, it cannot be transferred to other kinds of objects for use as a method. 原来Number有属于自身的原型valueOf方法,不是直接从Object.prototype上继承下来,类似的,Boolean、String也有自己的原型valueOf方法,归纳如下:
[b]类型    [/b] [b]是否有属于自己的原型valueOf方法[/b]
Undefined
Null
Number 有,Number.prototype.valueOf
String 有,String.prototype.valueOf
Boolean 有,Boolean.prototype.valueOf
Object -
此处之外,Array、Function并没有自己的原型valueOf方法,见规范说明: NOTE The Array prototype object does not have a valueOf property of its own; however, it inherits the valueOf property from the standard built-in Object prototype Object. The Function prototype object does not have a valueOf property of its own; however, it inherits the valueOf property from the Object prototype Object. 补充说明:Number.prototype.valueOf的内部转换规则比想的要略复杂些,此处不展开。 [b]啰啰嗦嗦说了一大通,现在还有两个问题存在疑惑[/b]: 1.关于ToObject,当参数为Function对象时,返回对象作何处理似乎没见到规范里明确说明,当前仅靠实验猜测(也有可能是我没找到) 2.valueOf的使用场景,实际开发中尚未见到有兄弟用过 最后的最后: 文中示例如有错漏,请指出;如觉得文章对您有用,可点击“推荐” :)
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部