欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS中令人發(fā)指的valueOf方法介紹

 更新時(shí)間:2013年02月22日 10:18:15   投稿:whsnow  
近期月報(bào)里提到了valueOf方法,興致來了翻了下ECMA5里關(guān)于valueOf方法的介紹真是令人發(fā)指啊,不可思議的朋友可以參考下啊

彭老濕近期月報(bào)里提到了valueOf方法,興致來了翻了下ECMA5里關(guān)于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.
規(guī)范里面的對(duì)于valueOf的解釋很短,大致為:調(diào)用ToObject方法(一個(gè)抽象方法,后面會(huì)講到),并將this的值作為參數(shù)傳入。

針對(duì)調(diào)用ToObject時(shí)傳入的不同參數(shù)(this),返回值分別如下:

1、this為宿主對(duì)象時(shí),返回值取決于瀏覽器的實(shí)現(xiàn),即不同瀏覽器的返回可能不同(關(guān)于宿主對(duì)象,可參考http://www.w3school.com.cn/js/pro_js_object_types.asp)
2、this不是宿主對(duì)象,則返回ToObject(this)的值 

參數(shù)類型  返回結(jié)果
Undefined拋出TypeError異常
Null拋出TypeError異常
Number創(chuàng)建一個(gè)Number對(duì)象,它內(nèi)部的初始值為傳入的參數(shù)值
String創(chuàng)建一個(gè)String對(duì)象,它內(nèi)部的初始值為傳入的參數(shù)值
Boolean創(chuàng)建一個(gè)Boolean對(duì)象,它內(nèi)部的初始值為傳入的參數(shù)值
Object返回傳入的參數(shù)(無轉(zhuǎn)換)


根據(jù)Object.prototype.valueOf的定義,以及抽象方法ToObject的描述,可得下表 

obj類型  Object.prototype.valueOf.call(obj)返回結(jié)果
Undefined拋出TypeError異常
Null拋出TypeError異常
NumberNumber類型的對(duì)象,值等于obj
StringString類型的對(duì)象,值等于obj
BooleanBoolean類型的對(duì)象,值等于obj
Objectobj對(duì)象本身


舉幾個(gè)具體的例子:

復(fù)制代碼 代碼如下:

var num = 123;
console.log(num.valueOf()); //輸出:123
console.log(num.valueOf()); //輸出:'number'

var unde = undefined;
console.log(Object.prototype.valueOf.call(unde)); //輸出:'TypeError: Cannot convert null to object'

var obj = {name:'casper'};
var linkObj = obj.valueOf();
linkObj.name = 'change';
console.log(linkObj.name); //輸出:'change' ...說明obj.valueOf()返回的是對(duì)象自身

實(shí)際上,上面沒有提到Array、Function對(duì)象,根據(jù)下面代碼可以猜想,當(dāng)Object.prototype.valueOf調(diào)用時(shí),參數(shù)為Array、Function類型的對(duì)象時(shí),返回的結(jié)果也為對(duì)象自身:
復(fù)制代碼 代碼如下:

var arr = [1, 2 ,3];
var linkArr = arr.valueOf();
linkArr[0] = ['casper'];
console.log(linkArr); //輸出:['casper', 2, 3]

var foo = function(){ return 1; };
var linkFoo = foo.valueOf();
linkFoo.test = 'casper';
console.log(linkFoo.test); //輸出:'casper'

看完上面的描述,是不是有種恍然大悟的感覺?如果是的話,恭喜你,可能你跟我一樣其實(shí)還沒完全理解透徹。

簡(jiǎn)單舉個(gè)例子,當(dāng)調(diào)用Object.prototype.valueOf的對(duì)象為數(shù)值類型時(shí),假設(shè)該對(duì)象是名稱為num,num很有可能通過下面兩種方式聲明:
復(fù)制代碼 代碼如下:

var num = 123; //通過對(duì)象字面量聲明console.log(typeof num); //輸出:'number'
var num = new Number(123); //通過構(gòu)造方法聲明console.log(typeof num); //輸出:'object'

關(guān)于返回值的說明,ECMA5里面原文如下

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()返回的應(yīng)該是個(gè)Number對(duì)象(非字面量聲明的那種),但實(shí)際上:
復(fù)制代碼 代碼如下:

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方法,歸納如下:

類型    是否有屬于自己的原型valueOf方法
Undefined
Null
Number有,Number.prototype.valueOf
String有,String.prototype.valueOf
Boolean有,Boolean.prototype.valueOf
Object-

此處之外,Array、Function并沒有自己的原型valueOf方法,見規(guī)范說明:

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.
補(bǔ)充說明:Number.prototype.valueOf的內(nèi)部轉(zhuǎn)換規(guī)則比想的要略復(fù)雜些,此處不展開。

啰啰嗦嗦說了一大通,現(xiàn)在還有兩個(gè)問題存在疑惑

1.關(guān)于ToObject,當(dāng)參數(shù)為Function對(duì)象時(shí),返回對(duì)象作何處理似乎沒見到規(guī)范里明確說明,當(dāng)前僅靠實(shí)驗(yàn)猜測(cè)(也有可能是我沒找到)
2.valueOf的使用場(chǎng)景,實(shí)際開發(fā)中尚未見到有兄弟用過
最后的最后:

文中示例如有錯(cuò)漏,請(qǐng)指出;如覺得文章對(duì)您有用,可點(diǎn)擊“推薦” :)

相關(guān)文章

最新評(píng)論