049_Number对象
1. Number對象
1.1. Number是對原始數(shù)字的包裝對象。
1.2. 創(chuàng)建Number對象的語法:
var myNum = new Number(value);1.3. 參數(shù)value是要創(chuàng)建的Number對象的數(shù)字。
1.4. 當(dāng)Number()和運算符new一起作為構(gòu)造函數(shù)使用時, 它返回一個新創(chuàng)建的Number對象。
2. Number對象屬性
3. Number對象方法
4. 使用字面量創(chuàng)建數(shù)字和Number對象創(chuàng)建數(shù)字
4.1. 使用字面量創(chuàng)建數(shù)字和Number對象創(chuàng)建數(shù)字
var num1 = new Number(10); // 使用Number對象創(chuàng)建數(shù)字 var num2 = 10; // 使用字面量創(chuàng)建數(shù)字4.2. 使用字面量創(chuàng)建數(shù)字和Number對象創(chuàng)建數(shù)字的不同
4.2.1. 使用字面量創(chuàng)建數(shù)字是原始值, 儲存在棧中。
4.2.2. 使用Number對象創(chuàng)建數(shù)字是對象(引用類型), 儲存在堆中。
4.2.3. 使用字面量創(chuàng)建數(shù)字和Number對象創(chuàng)建數(shù)字的內(nèi)存結(jié)構(gòu):
4.2.4. 使用Number對象創(chuàng)建數(shù)字比使用字面量創(chuàng)建數(shù)字復(fù)雜的多(就是因為Number對象多執(zhí)行了一個Number()構(gòu)造函數(shù)), 執(zhí)行效率低, 因此我們通常使用字面量創(chuàng)建數(shù)字。
4.2.5. 使用==運算符判斷使用字面量和Number對象創(chuàng)建的相同值的數(shù)字是否相等時, 結(jié)果是true, 因為數(shù)值相同。
4.2.6. 使用===運算符判斷使用字面量和Number對象創(chuàng)建的相同值的數(shù)字是否相等時, 結(jié)果是false, 因為雖然數(shù)值相同, 但類型不同, 一個是數(shù)字, 一個是對象。
5. 數(shù)字原始值可以使用Number對象的屬性和方法
5.1. 數(shù)字原始值, 比如10, 無法擁有屬性和方法, 因為它們不是對象。
5.2. 但是通過JavaScript, Number對象的屬性和方法也可用于原始數(shù)字, 因為在執(zhí)行屬性和方法時 JavaScript將原始數(shù)字視為偽對象。
6. Number構(gòu)造函數(shù)還有5個用作數(shù)字常量的占位符。這5個常量分別是MAX_VALUE(最大數(shù))、MIN_VALUE(最小數(shù))、POSITIVE_INFINITY(正無窮大)、NEGATIVE_INFINITY(負(fù)無窮大)和特殊的NaN(not a number)值。注意, 這些值是構(gòu)造函數(shù)Number()自身的屬性, 而不是單獨的某個Number對象的屬性。
var num1 = new Number(10); var num2 = 10;// 正確使用 document.write('Number.MAX_VALUE = ' + Number.MAX_VALUE + '<br />'); document.write('Number.MIN_VALUE = ' + Number.MIN_VALUE + '<br />'); document.write('Number.NaN = ' + Number.NaN + '<br />'); document.write('Number.NEGATIVE_INFINITY = ' + Number.NEGATIVE_INFINITY + '<br />'); document.write('Number.POSITIVE_INFINITY = ' + Number.POSITIVE_INFINITY + '<br />');// 錯誤使用 document.write('num1.MAX_VALUE = ' + num1.MAX_VALUE + '<br />'); document.write('num1.MIN_VALUE = ' + num1.MIN_VALUE + '<br />'); document.write('num1.NaN = ' + num1.NaN + '<br />'); document.write('num1.NEGATIVE_INFINITY = ' + num1.NEGATIVE_INFINITY + '<br />'); document.write('num1.POSITIVE_INFINITY = ' + num1.POSITIVE_INFINITY + '<br />');document.write('num2.MAX_VALUE = ' + num2.MAX_VALUE + '<br />'); document.write('num2.MIN_VALUE = ' + num2.MIN_VALUE + '<br />'); document.write('num2.NaN = ' + num2.NaN + '<br />'); document.write('num2.NEGATIVE_INFINITY = ' + num2.NEGATIVE_INFINITY + '<br />'); document.write('num2.POSITIVE_INFINITY = ' + num2.POSITIVE_INFINITY + '<br />');7. Number對象重寫了Object對象的constructor: ? Number()屬性, 和toLocaleString()、toString()、valueOf()這三個方法。
8. toLocaleString()方法
8.1. toLocaleString()方法可把一個Number對象轉(zhuǎn)換為本地格式的字符串。
8.2. 語法
numberObject.toLocaleString()8.3. 返回數(shù)字的字符串表示, 由實現(xiàn)決定, 根據(jù)本地規(guī)范進(jìn)行格式化, 可能影響到小數(shù)點或千分位分隔符采用的標(biāo)點符號。
9. valueOf()方法
9.1. valueOf()方法返回Number對象的原始值。
9.2. 語法
numberObject.valueOf()10. 例子
10.1. 代碼
<!DOCTYPE html> <html lang="zh-CN"><head><meta charset="utf-8" /><title>Number對象</title></head><body><script type="text/javascript">var num1 = new Number(3.1415926);var num2 = 3.1415926;document.write('num1 = ' + num1 + '<br />');document.write('num2 = ' + num2 + '<br />');document.write('(num1 == num2) = ' + (num1 == num2) + '<br />');document.write('(num1 === num2) = ' + (num1 === num2) + '<br />');document.write('(num1.valueOf() === num2) = ' + (num1.valueOf() === num2) + '<br />');document.write('(typeof num1) = ' + (typeof num1) + ', (typeof num1.valueOf()) = ' + (typeof num1.valueOf()) + '<br />');document.write('(typeof num2) = ' + (typeof num2) + ', (typeof num2.valueOf()) = ' + (typeof num2.valueOf()) + '<hr />');document.write('(num1.toString() === num2.toString()) = ' + (num1.toString() === num2.toString()) + '<br />');document.write('(typeof num1) = ' + (typeof num1) + ', (typeof num1.toString()) = ' + (typeof num1.toString()) + ', (num1.toString()) = ' + num1.toString() + '<br />');document.write('(typeof num2) = ' + (typeof num2) + ', (typeof num2.toString()) = ' + (typeof num2.toString()) + ', (num2.toString()) = ' + num2.toString() + '<br />');document.write('(num1.toLocaleString() === num2.toLocaleString()) = ' + (num1.toLocaleString() === num2.toLocaleString()) + '<br />');document.write('(typeof num1) = ' + (typeof num1) + ', (typeof num1.toLocaleString()) = ' + (typeof num1.toLocaleString()) + ', (num1.toLocaleString()) = ' + num1.toLocaleString() + '<br />');document.write('(typeof num2) = ' + (typeof num2) + ', (typeof num2.toLocaleString()) = ' + (typeof num2.toLocaleString()) + ', (num2.toLocaleString()) = ' + num2.toLocaleString() + '<hr />');document.write('Number.MAX_VALUE = ' + Number.MAX_VALUE + '<br />');document.write('Number.MIN_VALUE = ' + Number.MIN_VALUE + '<br />');document.write('Number.NaN = ' + Number.NaN + '<br />');document.write('Number.NEGATIVE_INFINITY = ' + Number.NEGATIVE_INFINITY + '<br />');document.write('Number.POSITIVE_INFINITY = ' + Number.POSITIVE_INFINITY + '<br />');</script></body> </html>10.2. 效果圖
總結(jié)
以上是生活随笔為你收集整理的049_Number对象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 048_String对象
- 下一篇: 050_Boolean对象